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

201 Views Asked by At

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

2

There are 2 best solutions below

2
On BEST ANSWER

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']
0
On

You may use 2 negative lookbehind assertions like this:

(?<!\bM )(?<!\bMme )\bet\b

RegEx Demo

A space character after M and Mme is important because in your input you have a space before et.

RegEx Details:

  • (?<!\bM ): Negative lookbehind to fail the match if we have "M " string before current position
  • (?<!\bMme ): Negative lookbehind to fail the match if we have "Mme " string before current position
  • \bet\b: Match complete word et