Search code examples
pythonregexregex-lookarounds

Negative lookahead not working after character range with plus quantifier


I am trying to implement a regex which includes all the strings which have any number of words but cannot be followed by a : and ignore the match if it does. I decided to use a negative look ahead for it.

/([a-zA-Z]+)(?!:)/gm
string: lame:joker

since i am using a character range it is matching one character at a time and only ignoring the last character before the : . How do i ignore the entire match in this case?

Link to regex101: https://regex101.com/r/DlEmC9/1


Solution

  • Do a word boundary check \b after the + to require it to get to the end of the word.

    ([a-zA-Z]+\b)(?!:)
    

    Here's an example run.