正则表达式三

来源:互联网 发布:星座软件哪个好 编辑:程序博客网 时间:2024/05/18 02:58

前后查找

(?=) 正向前查找
(?<=) 正向后查找

(?!) 负向前查找 取非
(?< !) 负向后查找 取非

  1. 正向前查找
str = @"http://www.forta.com/                    https://www.sdfol.com/                   ftp://ftp.forta.com";/* 字表达式(?=:)匹配:匹配到的:并没有出现在最终的匹配的结果里,只要找到:就行了*/pattern = @".+(?=:)";结果:httphttpsftp

2.正向后查找

str = @"1:$123.34        2:$3.1        3:$3999.0        4:$.        5:$32.";pattern = @"(?<=\$)\d+\.{1}[0-9]{1,2}";结果:$123.34$3.1$3999.0

3.负向后查找

str = @"I paid $30 for 100 apples,        50 oranges,and 60 pears,        I saved $5 on this order.";pattern = @"\b(?<!\$)\d+\b";结果:1005060/*如果不加\b,\b是单词分隔符,$30后面的  0也会匹配出来,应为这个0完全符合"(?<!\$)\d+"这个匹配*/pattern = @"(?<!\$)\d+";
原创粉丝点击