Search code examples
pythonpython-re

Partial string matching in python using re


Match all lowercase letters which are followed by two or more capital letters then three or more digits: (the capital letters and digits should not be included in the match) in python

I tried this but not working:[a-z]?![A-Z]{2,}[0-9]{3,}.


Solution

  • Use your current approach, but put the capital letters followed by numbers assertion into a positive lookahead:

    [a-z](?=[A-Z]{2,}[0-9]{3,})
    

    This pattern says to:

    [a-z]          match a lowercase letter
    (?=            then lookahead (but do NOT consume)
        [A-Z]{2,}  2 or more uppercase letters
        [0-9]{3,}  followed by 3 or more digits
    )
    

    Lookaheads assert, but not consume, in a regex pattern.