Question

I want a regex to match a number string of length 5, but should not match group of 2 in middle of string Eg. if my sample string is 12345 then i want 23 and 24 should not match in the string but should match 21, 22, 25

Should match 12145 12245 12567

Should not match 12345 12456

Can any please help me..

Was it helpful?

Solution

import re

pat = '\d(?!23|24)\d{4}'

rgx = re.compile(pat)

for x in ('12145','12245','12345','12456','12567'):
    print rgx.match(x)

result

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>

EDIT 1

If you need something a little more complex, that is to say excluding sucessions of 2 digits and successions of 3 digits starting at position 1:

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)

result

\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

.

EDIT 2

Showing what can be done:

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)

result

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top