Computer >> Computer tutorials >  >> Programming >> Python

How not to match a character after repetition in Python Regex?


^
  # Beginning of string
(?!
 # Do not match if
|2{1,2}5)
# Or there is one or two 2s followed by a 5
.*
 # Anything else is accepted


Example

The following code fulfils the conditions in given question

import re
foo = '2249678'
foo2 = '2259678'
result = re.findall(r'^(?!2{1,2}5).*', foo)
result2 = re.findall(r'^(?!2{1,2}5).*', foo2)
print result
print result2

Output

This gives the output

['2249678']
[]