首页 > 编程语言 > 详细

python3 正则匹配[^abc]和(?!abc)的区别(把多个字符作为一个整体匹配排除)

时间:2017-09-04 15:51:01      阅读:611      评论:0      收藏:0      [点我收藏+]

目的:把数字后面不为abc的字符串找出来

如1ab符合要求,2abc不符合要求

 1 str = 1ab
 2 out = re.match(r\d+(?!abc),str)
 3 
 4 str1 = 1abc
 5 out1 = re.match(r\d+(?!abc),str1)
 6 
 7 print(out:,out)
 8 print(out1:,out1)
 9 #
10 #out: <_sre.SRE_Match object; span=(0, 1), match=‘1‘>
11 #out1: None
12 #

如果把(?!abc)改为[^abc],效果如下:

 1 str = 1ab
 2 out3 = re.match(r\d+[^abc],str)
 3 
 4 str1 = 1abc
 5 out4 = re.match(r\d+[^abc],str1)
 6 
 7 print(out:,out3)
 8 print(out1:,out4)
 9 
10 #
11 #out3: None
12 #out4: None

 

python3 正则匹配[^abc]和(?!abc)的区别(把多个字符作为一个整体匹配排除)

原文:http://www.cnblogs.com/congyinew/p/7473835.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!