Variable-Width Lookbehind Issue in Python

regex module: variable-width lookbehind

In addition to the answer by HamZa, for any regex of any complexity in Python, I recommend using the outstanding regex module by Matthew Barnett. It supports infinite lookbehind—one of the few engines to do so, along with .NET and JGSoft.

This allows you to do for instance:

import regex
if regex.search("(?<!right |left )shoulder", "left shoulder"):
    print("It matches!")
else:
    print("Nah... No match.")

You could also use \s+ if you wished.

Output:

It matches!

Leave a Comment