1

例如,我想验证一个包含电话号码的字符串,以首先强制执行“+”后跟 2 位数字,然后是数字序列或连字符或空格的单个实例。字符串必须以数字结尾。

我目前有:

<xs:pattern value="\+\d{2}[- ]?[\d -]+[\d]"/>

但这并不限制字符串中空格或连字符的重复。如何做到这一点?

4

1 回答 1

2

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):

"+01 "
+01-
+4312345678912345678900

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

\+\d{2}\d*[- ]?\d+

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:

\+\d{2}(\d+|(\d*(-\d+| \d+)?))

[Not tested.]

于 2012-09-24T23:39:03.223 回答