I dont know how doable this is but I’m working on a datatype for an XSD and one of the things I’m trying to do is expand it to allow for a hyphen in a last name. So this should match Smith Fry and Safran-Foer. Additionally, I’d like to limit the length of the string being checked to no more than 100 (or 101) characters. My Regex originaly was:
<xsd:pattern value="[a-zA-Z ]{0,100}"/>
Now I know I can do something where I break this up and arbitrarily allow 50 characters on either side like:
<xsd:pattern value="[a-zA-Z ]{0,50}(\-)?[a-zA-Z ]{0,50}"/>
But that seems ungraceful. Is there any way to do something along the lines of:
<xsd:pattern value="[a-zA-Z (\-)?]{0,100}"/>
Another way of asking for what I’m looking for is ‘Match a string of characters between 0 and 100 long with no more than 1 hyphen in it’.
Thanks!
This is a swing at
'Match a string of characters between 0 and 100 long with no more than 1 hyphen in it'plus some additional constraints:I don’t think you could have the max length done in the pattern considering the syntax supported by XSD regex; it is easy, however, to combine it with a maxLength facet.
This is an XSD:
The pattern could be further refined to prohibit a hyphen surrounded by whitespace only, etc.
Valid XML:
Invalid XML (too many hyphens) and message:
Validation ERROR:
Error occurred while loading [], line 3 position 121The 'http://tempuri.org/XMLSchema.xsd:last-name' element is invalid - The value 'l-ast - name' is invalid according to its datatype 'String' - The Pattern constraint failed.
Invalid XML (longer than max, for tests I’ve used maxLength=14) and message:
Validation ERROR:
Error occurred while loading [], line 3 position 135The 'http://tempuri.org/XMLSchema.xsd:last-name' element is invalid - The value 'last - name that is longer' is invalid according to its datatype 'String' - The actual length is greater than the MaxLength value.