我希望正则表达式匹配长度为5的数字字符串,但不应该匹配字符串中间的2的组 例如。如果我的样本字符串是12345,那么我希望23和24在字符串中不匹配,但应匹配21,22,25
应该匹配 12145 12245 12567
不应该匹配 12345 12456
请帮助我..
答案 0 :(得分:0)
import re
pat = '\d(?!23|24)\d{4}'
rgx = re.compile(pat)
for x in ('12145','12245','12345','12456','12567'):
print rgx.match(x)
结果
12145 <_sre.SRE_Match object at 0x00AB01E0>
12245 <_sre.SRE_Match object at 0x00AB01E0>
12345 None
12456 None
12567 <_sre.SRE_Match object at 0x00AB01E0>
如果您需要更复杂的东西,也就是说从第1位开始排除2位数的连续数和3位数的连续数:
import re
tu2 = (23,0,24,78,2,85,84,32)
tu3 = (333,899)
pat = '\d(?!%s|%s)\d{4}' % ('|'.join(map('{:02}'.format,tu2)),
'|'.join(map('{:03}'.format,tu3)))
print pat,'\n'
rgx = re.compile(pat)
for x in ('12145','12245','12345','12456','12567',
'278455','13358','13337','18907','18995'):
print x,rgx.match(x)
结果
\d(?!23|00|24|78|02|85|84|32|333|899)\d{4}
12145 <_sre.SRE_Match object at 0x011DB528>
12245 <_sre.SRE_Match object at 0x011DB528>
12345 None
12456 None
12567 <_sre.SRE_Match object at 0x011DB528>
278455 None
13358 <_sre.SRE_Match object at 0x011DB528>
13337 None
18907 <_sre.SRE_Match object at 0x011DB528>
18995 None
展示可以做的事情:
import re
from collections import defaultdict
conditions = ( (2,2,(23,0,24,78,2)),
(1,2,(1,79) ),
(2,3,(333,899,8) ),
(1,4,(3333,3353))
)
conds = defaultdict(list)
for d,L,tu in conditions:
conds[d].extend(map(('{:0%s}' % L).format,tu))
pat = ''.join( '(?!\d{%s}(?:%s))' % (k-1,'|'.join(conds[k]))
for k in conds )
pat += '\d{5}'
print '\npat:\n',pat,'\n'
rgx = re.compile(pat)
for x in ('01655','02655','79443','78443',
'12145','12245','12345','12456','12567',
'278455','70245','13338','13348',
'18997','18905','60089','60189',
'33330','33530','33730'):
print x,rgx.match(x)
结果
pat:
(?!\d{0}(?:01|79|3333|3353))(?!\d{1}(?:23|00|24|78|02|333|899|008))\d{5}
01655 None
02655 <_sre.SRE_Match object at 0x011DB528>
79443 None
78443 <_sre.SRE_Match object at 0x011DB528>
12145 <_sre.SRE_Match object at 0x011DB528>
12245 <_sre.SRE_Match object at 0x011DB528>
12345 None
12456 None
12567 <_sre.SRE_Match object at 0x011DB528>
278455 None
70245 None
13338 None
13348 <_sre.SRE_Match object at 0x011DB528>
18997 None
18905 <_sre.SRE_Match object at 0x011DB528>
60089 None
60189 <_sre.SRE_Match object at 0x011DB528>
33330 None
33530 None
33730 <_sre.SRE_Match object at 0x011DB528>