string str = string.Empty;
string pattern = string.Empty;
1.
str = "sehiow sdhfoi hyunbar@126.com hofsdf;asdf hy123@1234.com sdhfoi.hyunuar@123.cn";
/*1.0 +匹配一个或多个,至少一个,不匹配0个
2.0 给一个字符集合家上+时,必须放在集合的外面
3.0 \w包括a-z,A-Z,0-9,_
*/
/*结果:
* hyunbar@126.com
* hy123@1234.com
* hyunuar@123.cn
*/
pattern = @"\w+@\w+\.\w+";
//hyunbar@126.com
//hy123@1234.com
//sdhfoi.hyunuar@123.cn
//[\w\.]和[\w.]是一样的,像.和+在字符集里使用时 不需要转义,将会被解释为普通的字符
// string pattern2 = @"[\w\.]+@[\w\.]+\.\w+";
pattern = @"[\w.]+@[\w.]+\.\w+";
//1.第一个字符必须是数字,字符,下划线
//2.*号匹配[\w.]字符集0个或者多个
pattern = @"\w+[\w.]*@[\w.]+\.\w+";
2.
str = @"thie url is http://www.baidu.com/, toisf connect user https://www.baidu.com/ insted";
//?号前面的s要么不出现,要不出现一次
pattern = @"https?://[\w./]+";
/*结果:
*http://www.baidu.com/
*https://www.baidu.com/
*/
3.
//匹配中间的空白行
str = @"
0,1,2
3,4,5
6,7,8";
//Windows要\r\n匹配空行
//Unix和linux只用\n匹配空行
pattern = @"[\r]?\n[\r]?\n";
4.{}
//1.0为重复匹配次数设定一个区间
//{}语法可以用来为重复匹配设定一个区间
str = @"4/8/03 10-6-2004 2/2/2 01-01-01";
pattern = @"\d{1,2}[-\/]\d{1,2}[-\/]\d{2,4}";
/*结果:4/8/03
10-6-20
01-01-01
*/
//2.0至少重复多少次{2,}至少匹配两次
//3.0为重复匹配设定一个精确的值如:{2}必须匹配两个
str=@"
1001:$231.40
1002:$2342.83
1003:$12.23
1004:$1.11";
pattern = @"\d+:\$\d{2,}\.\d{2}";
/*结果:
* 1001:$231.40
* 1002:$2342.83
* 1003:$12.23
*/
5.防止过度匹配
/* *===*?
* +===+?
* {n,}==={n,}?
*/
str = @"this offer is not availble to in <B>ak</B> and <B>HI</B>";
pattern = @"<[Bb]>.*?</[Bb]>";
/*<B>ak</B>
*<B>HI</B>
*/