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

PythonServer Side ProgrammingProgramming

^
  # 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']
[]
raja
Published on 02-Jan-2018 13:53:01
Advertisements