C#正则表达式整理备忘
有一段时间,正则表达式学习很火热很潮流,当时在CSDN一天就能看到好几个正则表达式的帖子,那段时间借助论坛以及Wrox Press出版的《C#字符串和正则表达式参考手册》学习了一些基础的知识,同时也为我在CSDN大概赚了1000分,今天想起来,去找《C#字符串和正则表达式参考手册》时,已经不知所踪了。现在用到正则的时候也比较少,把以前的笔记等整理一下,以志不忘。
(1)“@”符号
符下两ows表研究室的火热,当晨在“@”虽然并非C#正则表达式的“成员”,但是它经常与C#正则表达式出双入对。“@”表示,跟在它后面的字符串是个“逐字字符串”,不是很好理解,举个例子,以下两个声明是等效的:
string x="D:\\My Huang\\My Doc";
string y = @"D:\My Huang\My Doc";
事实上,如果按如下声明,C#将会报错,因为“\”在C#中用于实现转义,如“\n”换行:
string x = "D:\My Huang\My Doc";
(2)基本的语法字符。
\d 0-9的数字
\D \d的补集(以所以字符为全集,下同),即所有非数字的字符
\w 单词字符,指大小写字母、0-9的数字、下划线
\W \w的补集
\s 空白字符,包括换行符\n、回车符\r、制表符\t、垂直制表符\v、换页符\f
\S \s的补集
. 除换行符\n外的任意字符
[…] 匹配[]内所列出的所有字符
[^…] 匹配非[]内所列出的字符
下面提供一些简单的示例:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->stringi="\n";
stringm="3";
Regexr=newRegex(@"\D");
//同Regexr=newRegex("\\D");
//r.IsMatch(i)结果:true
//r.IsMatch(m)结果:false
stringi="%";
stringm="3";
Regexr=newRegex("[a-z0-9]");
//匹配小写字母或数字字符
//r.IsMatch(i)结果:false
//r.IsMatch(m)结果:true
(3)定位字符
“定位字符”所代表的是一个虚的字符,它代表一个位置,你也可以直观地认为“定位字符”所代表的是某个字符与字符间的那个微小间隙。
^ 表示其后的字符必须位于字符串的开始处
$ 表示其前面的字符必须位于字符串的结束处
\b 匹配一个单词的边界
\B 匹配一个非单词的边界
另外,还包括:\A 前面的字符必须位于字符处的开始处,\z 前面的字符必须位于字符串的结束处,\Z 前面的字符必须位于字符串的结束处,或者位于换行符前
下面提供一些简单的示例:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->stringi="Livefornothing,dieforsomething";
Regexr1=newRegex("^Livefornothing,dieforsomething$");
//r1.IsMatch(i)true
Regexr2=newRegex("^Livefornothing,dieforsome$");
//r2.IsMatch(i)false
Regexr3=newRegex("^Livefornothing,dieforsome");
//r3.IsMatch(i)true
stringi=@"Livefornothing,
dieforsomething";//多行
Regexr1=newRegex("^Livefornothing,dieforsomething$");
Console.WriteLine("r1matchcount:"+r1.Matches(i).Count);//0
Regexr2=newRegex("^Livefornothing,dieforsomething$",RegexOptions.Multiline);
Console.WriteLine("r2matchcount:"+r2.Matches(i).Count);//0
Regexr3=newRegex("^Livefornothing,\r\ndieforsomething$");
Console.WriteLine("r3matchcount:"+r3.Matches(i).Count);//1
Regexr4=newRegex("^Livefornothing,$");
Console.WriteLine("r4matchcount:"+r4.Matches(i).Count);//0
Regexr5=newRegex("^Livefornothing,$",RegexOptions.Multiline);
Console.WriteLine("r5matchcount:"+r5.Matches(i).Count);//0
Regexr6=newRegex("^Livefornothing,\r\n$");
Console.WriteLine("r6matchcount:"+r6.Matches(i).Count);//0
Regexr7=newRegex("^Livefornothing,\r\n$",RegexOptions.Multiline);
Console.WriteLine("r7matchcount:"+r7.Matches(i).Count);//0
Regexr8=newRegex("^Livefornothing,\r$");
Console.WriteLine("r8matchcount:"+r8.Matches(i).Count);//0
Regexr9=newRegex("^Livefornothing,\r$",RegexOptions.Multiline);
Console.WriteLine("r9matchcount:"+r9.Matches(i).Count);//1
Regexr10=newRegex("^dieforsomething$");
Console.WriteLine("r10matchcount:"+r10.Matches(i).Count);//0
Regexr11=newRegex("^dieforsomething$",RegexOptions.Multiline);
Console.WriteLine("r11matchcount:"+r11.Matches(i).Count);//1
Regexr12=newRegex("^");
Console.WriteLine("r12matchcount:"+r12.Matches(i).Count);//1
Regexr13=newRegex("$");
Console.WriteLine("r13matchcount:"+r13.Matches(i).Count);//1
Regexr14=newRegex("^",RegexOptions.Multiline);
Console.WriteLine("r14matchcount:"+r14.Matches(i).Count);//2
Regexr15=newRegex("$",RegexOptions.Multiline);
Console.WriteLine("r15matchcount:"+r15.Matches(i).Count);//2
Regexr16=newRegex("^Livefornothing,\r$\n^dieforsomething$",RegexOptions.Multiline);
Console.WriteLine("r16matchcount:"+r16.Matches(i).Count);//1
//对于一个多行字符串,在设置了Multiline选项之后,^和$将出现多次匹配。
stringi="Livefornothing,dieforsomething";
stringm="Livefornothing,dieforsomething";
Regexr1=newRegex(@"\bthing\b");
Console.WriteLine("r1matchcount:"+r1.Matches(i).Count);//0
Regexr2=newRegex(@"thing\b");
Console.WriteLine("r2matchcount:"+r2.Matches(i).Count);//2
Regexr3=newRegex(@"\bthing\b");
Console.WriteLine("r3matchcount:"+r3.Matches(m).Count);//1
Regexr4=newRegex(@"\bforsomething\b");
Console.WriteLine("r4matchcount:"+r4.Matches(i).Count);//1
//\b通常用于约束一个完整的单词
(4)重复描述字符
“重复描述字符”是体现C#正则表达式“很好很强大”的地方之一:
{n} 匹配前面的字符n次
{n,} 匹配前面的字符n次或多于n次
{n,m} 匹配前面的字符n到m次
? 匹配前面的字符0或1次
+ 匹配前面的字符1次或多于1次
* 匹配前面的字符0次或式于0次
以下提供一些简单的示例:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->stringx="1024";
stringy="+1024";
stringz="1,024";
stringa="1";
stringb="-1024";
stringc="10000";
Regexr=newRegex(@"^\+?[1-9],?\d{3}$");
Console.WriteLine("xmatchcount:"+r.Matches(x).Count);//1
Console.WriteLine("ymatchcount:"+r.Matches(y).Count);//1
Console.WriteLine("zmatchcount:"+r.Matches(z).Count);//1
Console.WriteLine("amatchcount:"+r.Matches(a).Count);//0
Console.WriteLine("bmatchcount:"+r.Matches(b).Count);//0
Console.WriteLine("cmatchcount:"+r.Matches(c).Count);//0
//匹配1000到9999的整数。
(5)择一匹配
C#正则表达式中的 (|) 符号似乎没有一个专门的称谓,姑且称之为“择一匹配”吧。事实上,像[a-z]也是一种择一匹配,只不过它只能匹配单个字符,而(|)则提供了更大的范围,(ab|xy)表示匹配ab或匹配xy。注意“|”与“()”在此是一个整体。下面提供一些简单的示例:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->stringx="0";
stringy="0.23";
stringz="100";
stringa="100.01";
stringb="9.9";
stringc="99.9";
stringd="99.";
stringe="00.1";
Regexr=newRegex(@"^\+?((100(.0+)*)|([1-9]?[0-9])(\.\d+)*)$");
Console.WriteLine("xmatchcount:"+r.Matches(x).Count);//1
Console.WriteLine("ymatchcount:"+r.Matches(y).Count);//1
Console.WriteLine("zmatchcount:"+r.Matches(z).Count);//1
Console.WriteLine("amatchcount:"+r.Matches(a).Count);//0
Console.WriteLine("bmatchcount:"+r.Matches(b).Count);//1
Console.WriteLine("cmatchcount:"+r.Matches(c).Count);//1
Console.WriteLine("dmatchcount:"+r.Matches(d).Count);//0
Console.WriteLine("ematchcount:"+r.Matches(e).Count);//0
//匹配0到100的数。最外层的括号内包含两部分“(100(.0+)*)”,“([1-9]?[0-9])(\.\d+)*”,这两部分是“OR”的关系,即正则表达式引擎会先尝试匹配100,如果失败,则尝试匹配后一个表达式(表示[0,100)范围中的数字)。
(6)特殊字符的匹配
下面提供一些简单的示例:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->stringx="\\";
Regexr1=newRegex("^\\\\$");
Console.WriteLine("r1matchcount:"+r1.Matches(x).Count);//1
Regexr2=newRegex(@"^\\$");
Console.WriteLine("r2matchcount:"+r2.Matches(x).Count);//1
Regexr3=newRegex("^\\$");
Console.WriteLine("r3matchcount:"+r3.Matches(x).Count);//0
//匹配“\”
stringx="\"";
Regexr1=newRegex("^\"$");
Console.WriteLine("r1matchcount:"+r1.Matches(x).Count);//1
Regexr2=newRegex(@"^""$");
Console.WriteLine("r2matchcount:"+r2.Matches(x).Count);//1
//匹配双引号
(7)组与非捕获组
以下提供一些简单的示例:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->stringx="Livefornothing,dieforsomething";
stringy="Livefornothing,dieforsomebody";
Regexr=newRegex(@"^Live([a-z]{3})no([a-z]{5}),die\1some\2$");
Console.WriteLine("xmatchcount:"+r.Matches(x).Count);//1
Console.WriteLine("ymatchcount:"+r.Matches(y).Count);//0
//正则表达式引擎会记忆“()”中匹配到的内容,作为一个“组”,并且可以通过索引的方式进行引用。表达式中的“\1”,用于反向引用表达式中出现的第一个组,即粗体标识的第一个括号内容,“\2”则依此类推。
stringx="Livefornothing,dieforsomething";
Regexr=newRegex(@"^Liveforno([a-z]{5}),dieforsome\1$");
if(r.IsMatch(x))
{
Console.WriteLine("group1value:"+r.Match(x).Groups[1].Value);//输出:thing
}
//获取组中的内容。注意,此处是Groups[1],因为Groups[0]是整个匹配的字符串,即整个变量x的内容。