I am working on creating an XSD for a web service that will take in an ID number as an element in the XML. These ID numbers consist of 10 consecutive digits ([0-9]{10}), but I was trying to create a regular expression that could exclude certain elements from this range.
For example, here is the restriction I have currently in my XSD:
<xsd:restriction base="xsd:string">
<xsd:pattern value="[0-9]{10}" />
</xsd:restriction>
I need the restriction to allow a string of [0-9]{10} that doesn’t fit the following IDs:
All 0's: [0]{10}
Starting with 6: [6][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
Starting with 000: [0][0][0][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
Starting with 999: [9][9][9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
Ends with 2 0's: [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0][0]
4 0's in Middle: [0-9][0-9][0-9][0][0][0][0][0-9][0-9][0-9]
Is this possible to do from within the XSD or regular expression?
Thanks.
I’d rephrase your restrictions a bit:
6.The first restriction, an ID only consisting of zeroes, is actually included in the two last restrictions.
The first restriction can be expressed by a set of allowed characters that does not include
6, i.e.[0-57-9].For the other restrictions, a straightforward solution is to start at the beginning of a section that must not consist only of zeroes and assume a non-zero digit; if that assumption is true, the remaining digits may include zeroes; otherwise the first digit in that section must be a zero and for the remaining characters, this rule can be repeated recursively until only one character is left:
([1-9][0-9]{3}|0(... repeat for three digits, then two digits, ...))Therefore, a suitable RegEx would be:
Update: The additional restrictions require the following:
0.9.This can be included the same way as above, accepting either anything except
0and9, or either of these two numbers:The new part is in the front of the expression:
So,
0nor with a9. In that case, there are no restrictions for the next two digits.0. In that case, one of the next two digits must not be a zero, either the first one or the second one.9. In that case, one of the next two digits must not be a nine, either the first one or the second one.