Search code examples
pythonregexpython-re

How to match a string that a specific word doesn't comes before it?


I trying to match the string ARIBABA only if .get doesn't comes before it.

Example:

ARIBABA = config.get('SOMETHING', 'ARIBABA').lower()

I've tried this (down below) but it just doesn't match anything.

^(.get)\bARIBABA\b


Solution

  • You must use PyPi regex library here:

    import regex
    s = "ARIBABA = config.get('SOMETHING', 'ARIBABA').lower()"
    p = r"(?<!\.get\b.*)\bARIBABA\b"
    print(regex.findall(p, s))
    

    See Python proof.

    EXPLANATION

    --------------------------------------------------------------------------------
      (?<!                     look behind to see if there is not:
    --------------------------------------------------------------------------------
        \.                       '.'
    --------------------------------------------------------------------------------
        get                      'get'
    --------------------------------------------------------------------------------
        \b                       the boundary between a word char (\w)
                                 and something that is not a word char
    --------------------------------------------------------------------------------
        .*                       any character except \n (0 or more times
                                 (matching the most amount possible))
    --------------------------------------------------------------------------------
      )                        end of look-behind
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      ARIBABA                  'ARIBABA'
    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char