我有一些旧的.NET代码,用于搜索sql字符串中的参数
System.Text.RegularExpressions.MatchCollection collection =
System.Text.RegularExpressions.Regex.Matches(icmd.CommandText,
"(#,?,:)[a-zA-Z0-9]*_*");
我不知道它是如何匹配的。任何人都可以解释一下吗?
答案 0 :(得分:6)
见下图解释它。
更新:在下面的代码#,,:abc12_
中匹配。
System.Text.RegularExpressions.MatchCollection collection =
System.Text.RegularExpressions.Regex.Matches("#,,:abc12_", "(#,?,:)[a-zA-Z0-9]*_*");
答案 1 :(得分:3)
有关正则表达式的任何疑问,请参阅 this 。
在(#,?,:)[a-zA-Z0-9]*_*
1st Capturing group (#,?,:)
# matches the character # literally
,? matches the character , literally
Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
,: matches the characters ,: literally
[a-zA-Z0-9]* match a single character present in the list below
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_* matches the character _ literally
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
表示:它可以匹配#,,:anycharactorstring
,#,,:anycharactorstring_
,#,:anycharactorstring
,#,:anycharactorstring_____
等。