[英]Regex is not matching
How can I write a RegEx expression to match the number begin with 090 or 091 or 0123 or 0168 or 0199 or 0124 and the length between 10 to 11 digits? 如何编写正则表达式以匹配以090或091或0123或0168或0199或0124开头的数字以及10到11位数字之间的长度?
I try this but not true 我尝试这个但不是真的
@"^(090|091|0123|0168|0199|0124)\d{7,8}$"
The regex itself looks mostly OK, although of course it will allow 12-digit numbers, too (a four-digit start, followed by 8 further digits). 正则表达式本身看起来似乎还可以,尽管它当然也可以允许使用12位数字(以4位数字开头,然后是8位数字)。 To change that, I propose this:
为了改变这一点,我建议这样做:
foundMatch = Regex.IsMatch(subjectString,
@"^ # Start of string
(?=.{10,11}$) # Assert 10-11 character length
0 # Start with matching a 0
(?:90|91|123|168|199|124) # then one of the alternatives
[0-9]* # then fill the rest with digits.
$ # End of string",
RegexOptions.IgnorePatternWhitespace);
If you want to find numbers like that in a longer string, not validate a string, then use 如果要在较长的字符串中查找类似的数字,而不是验证字符串,请使用
resultString = Regex.Match(subjectString,
@"\b # Start of number
(?=[0-9]{10,11}\b) # Assert 10-11 character length
0 # Match 0
(?:90|91|123|168|199|124) # then one of the alternatives
[0-9]* # then fill the rest with digits
\b # End of number",
RegexOptions.IgnorePatternWhitespace).Value;
(assuming that the numbers are surrounded by non-alphanumeric characters). (假设数字用非字母数字字符包围)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.