有一段时间正则表达式学习很火热很潮流当时在CSDN一天就能看到好几个正则表达式的帖子那段时间借助论坛以及Wrox Press出版的《C#字符串和正则表达式参考手册》学习了一些基础的知识同时也为我在CSDN大概赚了分今天想起来去找《C#字符串和正则表达式参考手册》时已经不知所蹤了现在用到正则的时候也比较少把以前的笔记等整理一下以志不忘
()@符号符下两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;
()基本的语法字符
\d 的数字\D \d的补集(以所以字符为全集下同)即所有非数字的字符\w 单词字符指大小写字母的数字下划线\W \w的补集\s 空白字符包括换行符\n回车符\r制表符\t垂直制表符\v换页符\f \S \s的补集 除换行符\n外的任意字符[…] 匹配[]内所列出的所有字符[^…] 匹配非[]内所列出的字符下面提供一些简单的示例
string i = \n;
string m = ;
Regex r = new Regex(@\D);
//同Regex r = new Regex(\\D);
//rIsMatch(i)结果true
//rIsMatch(m)结果false
string i = %;
string m = ;
Regex r = new Regex([az]);
//匹配小写字母或数字字符
//rIsMatch(i)结果false
//rIsMatch(m)结果true
()定位字符定位字符所代表的是一个虚的字符它代表一个位置你也可以直观地认为定位字符所代表的是某个字符与字符间的那个微小间隙
^ 表示其后的字符必须位于字符串的开始处$ 表示其前面的字符必须位于字符串的结束处\b 匹配一个单词的边界\B 匹配一个非单词的边界另外还包括\A 前面的字符必须位于字符处的开始处\z 前面的字符必须位于字符串的结束处\Z 前面的字符必须位于字符串的结束处或者位于换行符前下面提供一些简单的示例
string i = Live for nothingdie for something;
Regex r = new Regex(^Live for nothingdie for something$);
//rIsMatch(i) true
Regex r = new Regex(^Live for nothingdie for some$);
//rIsMatch(i) false
Regex r = new Regex(^Live for nothingdie for some);
//rIsMatch(i) true
string i = @Live for nothing
die for something;//多行
Regex r = new Regex(^Live for nothingdie for something$);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothingdie for something$ RegexOptionsMultiline);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothing\r\ndie for something$);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothing$);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothing$ RegexOptionsMultiline);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothing\r\n$);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothing\r\n$ RegexOptionsMultiline);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothing\r$);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothing\r$ RegexOptionsMultiline);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^die for something$);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^die for something$ RegexOptionsMultiline);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex($);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^ RegexOptionsMultiline);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex($ RegexOptionsMultiline);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(^Live for nothing\r$\n^die for something$ RegexOptionsMultiline);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
//对于一个多行字符串在设置了Multiline选项之后^和$将出现多次匹配
string i = Live for nothingdie for something;
string m = Live for nothingdie for some thing;
Regex r = new Regex(@\bthing\b);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(@thing\b);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
Regex r = new Regex(@\bthing\b);
ConsoleWriteLine(r match count: + rMatches(m)Count);//
Regex r = new Regex(@\bfor something\b);
ConsoleWriteLine(r match count: + rMatches(i)Count);//
//\b通常用于约束一个完整的单词
()重复描述字符重复描述字符是体现C#正则表达式很好很强大的地方之一{n} 匹配前面的字符n次{n} 匹配前面的字符n次或多于n次{nm} 匹配前面的字符n到m次? 匹配前面的字符或次+ 匹配前面的字符次或多于次* 匹配前面的字符次或式于次以下提供一些简单的示例
string x = ;
string y = +;
string z = ;
string a = ;
string b=;
string c = ;
Regex r = new Regex(@^\+?[]?\d{}$);
ConsoleWriteLine(x match count: + rMatches(x)Count);//
ConsoleWriteLine(y match count: + rMatches(y)Count);//
ConsoleWriteLine(z match count: + rMatches(z)Count);//
ConsoleWriteLine(a match count: + rMatches(a)Count);//
ConsoleWriteLine(b match count: + rMatches(b)Count);//
ConsoleWriteLine(c match count: + rMatches(c)Count);//
//匹配到的整数