I would like a Python regular expression that matches a given word that\'s not between simple quotes. I\'ve tried to use the (?! ...)
but without success.
How about this regular expression:
>>> s = '''var foe = 10;
foe = "";
dark_vador = 'bad guy'
' I\m your father, foe ! '
bar = thingy + foe'''
>>>
>>> re.findall(r'(?!\'.*)foe(?!.*\')', s)
['foe', 'foe', 'foe']
The trick here is to make sure the expression does not match any string with leading and trailing '
and to remember to account for the characters in between, thereafter .*
in the re expression.