[英]How to set repeat regular expression?
I have regular expression ^\\d{5}$|^\\d{5}-\\d{4}*$"
it checked US zip. But I need check " zip, zip, zip
" how to do this? 我有正则表达式
^\\d{5}$|^\\d{5}-\\d{4}*$"
它检查了美国邮政编码。但我需要检查” zip, zip, zip
“如何做到这一点?
I tried this ^(\\d{5}$|^\\d{5}-\\d{4},)*$
but it not work 我试过这个
^(\\d{5}$|^\\d{5}-\\d{4},)*$
但它不起作用
Try 尝试
((^|, )(\d{5}|\d{5}-\d{4}))*$
Tester: http://regexr.com?36297 测试人员: http : //regexr.com?36297
Each match must be preceded by (^|, )
, so by the beginning of the string or a ,
(comma space) 每个匹配必须以
(^|, )
开头,所以在字符串的开头或a ,
(逗号空格)
Note that you shouldn't use the \\d
in .NET, because ٠١٢٣٤
are \\d
! 请注意,您不应该在.NET中使用
\\d
,因为٠١٢٣٤
是\\d
! (in .NET \\d
includes non-ASCII Unicode digits). (在.NET
\\d
包含非ASCII Unicode数字)。 [0-9]
is normally better. [0-9]
通常更好。
The expression you appear to need is: 您似乎需要的表达式是:
^\d{5}(|-\d{4})(,\d{5}(|-\d{4}))*$
The one you were attempting to write was: 你试图写的是:
^(\d{5}|\d{5}-\d{4},)*$
but that would require every ZIP to have a trailing comma, which the very last one would not have had. 但这需要每个ZIP都有一个尾随的逗号,这是最后一个没有的。
Breaking down the answer given, 打破给出的答案,
\\d{5}(|-\\d{4})
is a variant of your original, but simply making the -1234 optional. \\d{5}(|-\\d{4})
是原始版本的变体,但只需将-1234选为可选项。 (,\\d{5}(|-\\d{4}))*
is the first regular expression preceded by a comma, and allowed zero or more times. (,\\d{5}(|-\\d{4}))*
是第一个以逗号开头的正则表达式,允许零次或多次。 I would use this for speed: 我会用它来加速:
^\d{5}(?:-\d{4})?(?:,\s*\d{5}(?:-\d{4})?)*$
expanded 扩大
^
\d{5}
(?: - \d{4} )?
(?:
, \s* \d{5}
(?: - \d{4} )?
)*
$
and this for speed/flexibility: 这对于速度/灵活性:
^\s*\d{5}(?:\s*-\s*\d{4})?(?:\s*,\s*\d{5}(?:\s*-\s*\d{4})?)*\s*$
expanded 扩大
^
\s*
\d{5}
(?: \s* - \s* \d{4} )?
(?:
\s* , \s* \d{5}
(?: \s* - \s* \d{4} )?
)*
\s*
$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.