C# 正则表达式不匹配
如何编写正则表达式以匹配以090或091或0123或0168或0199或0124开头的数字以及10到11位之间的长度 我试过了,但不是真的C# 正则表达式不匹配,c#,regex,C#,Regex,如何编写正则表达式以匹配以090或091或0123或0168或0199或0124开头的数字以及10到11位之间的长度 我试过了,但不是真的 @"^(090|091|0123|0168|0199|0124)\d{7,8}$" 正则表达式本身看起来基本上还可以,当然它也允许使用12位数字(4位数字开头,后面是8位数字)。为了改变这一点,我提议: foundMatch = Regex.IsMatch(subjectString, @"^ # St
@"^(090|091|0123|0168|0199|0124)\d{7,8}$"
正则表达式本身看起来基本上还可以,当然它也允许使用12位数字(4位数字开头,后面是8位数字)。为了改变这一点,我提议:
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);
如果您希望在较长的字符串中查找类似的数字,而不是验证字符串,则使用
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;
(假设数字被非字母数字字符包围)。正则表达式本身看起来基本正常,当然它也允许12位数字(4位数字开头,后面是8位数字)。为了改变这一点,我提议:
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);
如果您希望在较长的字符串中查找类似的数字,而不是验证字符串,则使用
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;
(假设数字被非字母数字字符包围)。您的尝试是什么?是什么把你绊倒了?哇,我看到了很多更糟糕的问题还没有解决,第一次我会投票重新解决。你的正则表达式看起来不错,你能把代码贴出来吗?你怎么称呼它,数据看起来如何?你是怎么尝试的?是什么把你绊倒了?哇,我看到了很多更糟糕的问题还没有解决,第一次我会投票重新解决。你的正则表达式看起来不错,你能把代码贴出来吗?你怎么称呼它,数据看起来如何?