Search code examples
python-3.xregexnegative-lookbehind

Using Negative lookbehind with or operator (not preceded by multiple words)


I've a problem with my regex in Python.

et may only match if it is not preceded by m or mme

string: M et Mme Du Pont or Mme et M Du Pont

regex: '\b(?<!m|mme)\bet\b'

error: look-behind requires fixed-with patterd

How can i fix this?

'\b(?<!m)(?<!mme)\bet\b' doesn't seem to work either


Solution

  • Apart from the error, there is a space in the example data after m and mme which is missing in the pattern.

    Another option is to use the regex PyPi module with a single lookbehind asserting what is directly at the left is m, optionally followed by me and followed by a space.

    \b(?<!m(?:me)? )et\b
    

    Regex demo | Python using regex module demo

    For example

    import regex
    
    pattern = r"\b(?<!m(?:me)? )et\b"
    s = ("M et Mme Du Pont or Mme et M Du Pont\n"
                "et\n"
                "met")
    
    print(regex.findall(pattern, s, regex.IGNORECASE))
    

    Output

    ['et']