For example, I want to validate a string containing a phone number to enforce first a ‘+’ followed by 2 digits, then a sequence of either digits or a single instance of either a hyphen or a space. The string must end with a digit.
I currently have:
<xs:pattern value="\+\d{2}[- ]?[\d -]+[\d]"/>
but this does not restrict repetition of the spaces or hyphens within the string. How can this be achieved?
You say “… then a sequence of either digits or a single instance of either a hyphen or a space” — take literally, that suggests that the following are all acceptable (I quote the strings when they end with a space):
Looking at your code, I guess that you were speaking informally, and what you want could better be described as a plus, two digits, and then a sequence of digits interrupted by at most one hyphen or space, which must not be final. If that’s an accurate paraphrase, you might try
If you want to allow up to one hyphen and up to one space, it gets a lot more complicated, but it’s still expressible. Assuming you don’t want an empty sequence of digits to be acceptable:
[Not tested.]