正则表达式入门(c#)
本文是对该教程的学习练习
http://www.jb51.net/tools/zhengze.html
注:正则符号转义和普通的转义一样,加反斜杠,比如[ 变成 \[
正则表达式符号和转义符号最好用+号连起来。
关于在vs里用正则表达式替换需要分组,其实很简单用括号括起来就算一个组了
匹配到的数据都在Groups里面,C#提供了匹配,替换等功能,具体msdn
1.\bContent\b
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void Main(string[] args) { string str = "Act game - Uncharted3, act Game - God of war"; Regex rex = new Regex(@"\bact\b"); var result = rex.Match(str); if (result.Success) { var tmp = result.Index; Console.WriteLine(tmp); } else { Console.WriteLine("failure"); } Console.Read(); }
输出结果:23。
第一个act,a是大写。没做大小写匹配,所以正则匹配到的是索引23的那个act.
前面的\b是匹配开始处,后面的是匹配结束处。开始处和结束处指空格
比如只匹配开始处就是\bContent
2.\bContent1\b.*\bContent2\b
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void Main(string[] args) { string str = "rpg game - Legend of Heroes, act game - Uncharted3, act Game - God of war"; Regex rex = new Regex(@"\bact\b.*\bUncharted3\b"); var result = rex.Match(str); if (result.Success) { var tmp = result.Index; Console.WriteLine(tmp); } else { Console.WriteLine("failure"); } Console.Read(); }
检测关键字1后面是否跟着关键字2
输出结果:29。
但是遇到多个和前缀相同的字串,就会出问题。
如果不是查找单词,可以去掉\b。Content1.*Content2
3.0\d\d-\d\d\d\d\d\d\d\d
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string str = "the xxxx, xxxx, 021-88888888"; Regex rex = new Regex(@"0\d\d-\d\d\d\d\d\d\d\d"); ...
输出结果16
算是占位符,匹配电话号码啥的。代码后面都一样就省略掉。
\d是匹配数字
4.{count}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string str = "the xxxx, xxxx, 021-88888888"; Regex rex = new Regex(@"0\d{2}-\d{8}"); ...
输出结果16
上面那种写法的优化版。
5.{min,max}
\d是数字,\d{x}是匹配长度为x的数字,\d{x1,x2}是匹配长度最小为x1,最大为x2的数字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void Main(string[] args) { string str = "Act game - Uncharted3, act Game - God of war qwe 123456 asd"; Regex rex = new Regex(@"\d{5,12}"); var result = rex.Match(str); if (result.Success) { var tmp = result.Index; Console.WriteLine(tmp); } else { Console.WriteLine("failure"); } Console.Read(); }
输出结果为50
6.\s和\w
\s是匹配空格,花括号内容同上
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void Main(string[] args) { string str = "Act game - Uncharted3, act Game - God of war qwe asd"; Regex rex = new Regex(@"\s{5,12}"); var result = rex.Match(str); if (result.Success) { var tmp = result.Index; Console.WriteLine(tmp); } else { Console.WriteLine("failure"); } Console.Read(); }
输出结果为48
\w是匹配字母或数字或下划线或汉字。和\s差不多,不做示范了。
7.^和$
用于匹配整个字符串,通常注册帐号的时候,非常有用
失败,因为是整串匹配:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string str = "QQ: 123456"; Regex rex = new Regex(@"^\d{5,12}$"); var result = rex.Match(str); if (result.Success) { var tmp = result.Index; Console.WriteLine(tmp); } else { Console.WriteLine("failure"); } Console.Read();
匹配成功,返回0:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string str = "123456"; Regex rex = new Regex(@"^\d{5,12}$"); var result = rex.Match(str); if (result.Success) { var tmp = result.Index; Console.WriteLine(tmp); } else { Console.WriteLine("failure"); } Console.Read();
8. * + ? {n,}
* 重复零次或更多次
+ 重复一次或更多次
? 重复零次或一次
{n,} 重复n次或更多次
*和?测试的时候返回的总是0。暂时先搁一边
+,测试正常,返回4
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
static void Main(string[] args) { string str = "QQ: 111a"; Regex rex = new Regex(@"\d+\w"); var result = rex.Match(str); if (result.Success) { var tmp = result.Index; Console.WriteLine(tmp); } else { Console.WriteLine("failure"); } Console.Read(); }
{n,} 这里设置数字重复2次或者更多,返回值为4
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
string str = "QQ: 111a"; Regex rex = new Regex(@"\d{2,}\w"); var result = rex.Match(str); if (result.Success) { var tmp = result.Index; Console.WriteLine(tmp); } else { Console.WriteLine("failure"); } Console.Read();