码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-04 15:56:52      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:none   python3   目的   bsp   match   str1   blog   one   nbsp   

目的:把数字后面不为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)的区别(把多个字符作为一个整体匹配排除)

标签:none   python3   目的   bsp   match   str1   blog   one   nbsp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!