C# 正则表达式强制量词是有效的
我正在努力使我的名字首字母的正则表达式验证生效 我有以下代码:C# 正则表达式强制量词是有效的,c#,.net,regex,C#,.net,Regex,我正在努力使我的名字首字母的正则表达式验证生效 我有以下代码: // S.P. of Sp.A. string voorletters = "S.P.aAa"; // This should be invalid string regexPattern = "^([A-Z]{1}[.]|[A-Z]{1}[a-z]{1,}[.])*"; var isMatch = Regex.IsMatch(voorletters, regexPattern); 下面的示例应该无效,但不匹配,因此将通过。
// S.P. of Sp.A.
string voorletters = "S.P.aAa"; // This should be invalid
string regexPattern = "^([A-Z]{1}[.]|[A-Z]{1}[a-z]{1,}[.])*";
var isMatch = Regex.IsMatch(voorletters, regexPattern);
下面的示例应该无效,但不匹配,因此将通过。
如何强制使用量词(*)进行验证?尝试以下模式:
"^([A-Z]{1}[.]|[A-Z]{1}[a-z]{1,}[.]??)*$"
末尾的$是必需的,因为在您的用例中,匹配的部分实际上只是“S.p.”。第二个圆点后面的“?”是可选的,这取决于您在实践中想要什么-S.P.Aa是否有效?尝试以下模式:
"^([A-Z]{1}[.]|[A-Z]{1}[a-z]{1,}[.]??)*$"
末尾的$是必需的,因为在您的用例中,匹配的部分实际上只是“S.p.”。第二个圆点后面的“?”是可选的,这取决于您在实践中想要什么-S.P.Aa是否有效?尝试以下方法:
^([A-Z][a-z]?\.)*$
试试这个:
^([A-Z][a-z]?\.)*$
尝试一下:
^([A-Z][a-z]*\.)+$
说明:
The regular expression:
(?-imsx:^([A-Z][a-z]*\.)+$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1 (1 or more times
(matching the most amount possible)):
----------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
----------------------------------------------------------------------
[a-z]* any character of: 'a' to 'z' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
)+ end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
尝试一下:
^([A-Z][a-z]*\.)+$
说明:
The regular expression:
(?-imsx:^([A-Z][a-z]*\.)+$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
( group and capture to \1 (1 or more times
(matching the most amount possible)):
----------------------------------------------------------------------
[A-Z] any character of: 'A' to 'Z'
----------------------------------------------------------------------
[a-z]* any character of: 'a' to 'z' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
)+ end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
你说的
强制使用量词是什么意思?
末尾的量词(the*)使其成为下一个输入,因此下一个输入是前一组的重复。无论下一个组是什么都无关紧要,因为第一个组经过验证,IsMatch返回true。我不希望这样,因为我希望所有组都得到验证。如果您提供一些示例,说明哪些字符串应该匹配,哪些字符串不应该匹配,这会有所帮助。强制使用量词是什么意思?
?末尾的量词(the*)使下一个输入重复上一组。无论下一个组是什么都无关紧要,因为第一个组经过验证,IsMatch返回true。我不希望出现这种情况,因为我希望验证所有组。如果您提供一些示例,说明哪些字符串应该匹配,哪些字符串不应该匹配,这会有所帮助。这与S.P.Aa
不匹配。谢谢,此解决方案确实很好用。(也稍微短一点:)@AlexFilipovici结尾处始终需要一个点。这与S.P.Aa
不匹配。谢谢,这个解决方案确实很好用。(也稍微短一点:)@AlexFilipovici结尾总是需要一个点。