欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

正则表达式匹配 非XXX的行

程序员文章站 2022-05-19 11:18:54
1111111111111  前边有内容,不定123.123.123.10后边有内容,不定  3333333333333  444444444...


1111111111111 
前边有内容,不定123.123.123.10后边有内容,不定 
3333333333333 
4444444444444 


如何匹配"非:.+123.123.123.10.+ " 
行 

匹配结果是, 
1111111111111 

3333333333333 
4444444444444

结论:

^(?!.*123.123.123.10).*$ 
或c#里这么操作:

 textbox2.text = regex.replace(textbox1.text, @"\n.*123\.123\.123\.10.*\n", "\n\r\n");
string result = regex.replace(str, @"^.*?123\.123\.123\.10.*$", "", regexoptions.multiline);
regex reg =new regex(@"(?<=^|\r\n)(?!123\.123\.123\.10).+"); 

总结:

匹配非“非内容”的行的表达式应该写成:^(?!.*非内容).*$ 


=================================== 
另,再来个例子: 
2008-07-14 15:44:40 w3svc491 60.27.236.4 get /item/how-to-connection-my-computer-flow-ip-yongfa365.html - 80 - 74.6.22.106 mozilla/5.0+(compatible;+yahoo!+slurp;+http://help.yahoo.com/help/us/ysearch/slurp) 200 0 0 
2008-07-14 15:44:51 w3svc491 60.27.236.4 get /item/clear.bat-system-windows-98-xp-2003-yongfa365.html - 80 - 124.73.140.102 mozilla/4.0+(compatible;+msie+6.0;+windows+nt+5.0) 200 0 64 
2008-07-14 15:44:51 w3svc491 60.27.236.4 get /item/clear.bat-system-windows-98-xp-2003-yongfa365.html - 80 - 124.73.140.102 mozilla/4.0+(compatible;+msie+6.0;+windows+nt+5.0) 206 0 64 
2008-07-14 15:47:11 w3svc491 60.27.236.4 get /articles.xml - 80 - 65.214.44.28 bloglines/3.1+(http://www.bloglines.com;+1+subscriber) 200 0 64 
2008-07-14 15:47:47 w3svc491 60.27.236.4 get /item/sql-server-store-more-than-8000-writetext-updatetext-yongfa365.html - 80 - 59.125.118.13 trend+micro+web+protection+add-on+1.10.1144 200 0 0 

找出上而面的行里的,不是以“200 0 64”结尾的行,然后删除

^(?!.*200 0 64).*$ 

而我一直是这么测试的: 
^(?!.*200 0 64)$ 
没有后边的.* 因为我觉得后边已经没有内容了,所以没写 .* 而不写就不对,写了才行,哪位高人可以再指点下。