正则表达式(#,?,:)[a-zA-Z0-9] * _ *是什么意思?

时间:2014-01-09 07:19:29

标签: .net regex

我有一些旧的.NET代码,用于搜索sql字符串中的参数

System.Text.RegularExpressions.MatchCollection collection =
System.Text.RegularExpressions.Regex.Matches(icmd.CommandText, 
"(#,?,:)[a-zA-Z0-9]*_*");

我不知道它是如何匹配的。任何人都可以解释一下吗?

2 个答案:

答案 0 :(得分:6)

见下图解释它。

enter image description here

更新:在下面的代码#,,: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_____等。