正则表达式一

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>
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值