1. 程式人生 > >python3 正則匹配[^abc]和(?!abc)的區別(把多個字符作為一個整體匹配排除)

python3 正則匹配[^abc]和(?!abc)的區別(把多個字符作為一個整體匹配排除)

mat obj python str 效果 目的 str1 排除 blog

目的:把數字後面不為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)的區別(把多個字符作為一個整體匹配排除)