How can I substitute all occurrence of a certain string NOT after a specific character in Python?
For example, I want to substitute all occurrence of abc
NO
result = re.sub("(?<!x)abc", "def", subject)
(?<!x)
asserts that what precedes is not x
abc
matches abc
def
Reference
With a negative lookbehind assertion (?<!...)
(i.e. not preceded by):
(?<!x)abc
In a replacement:
re.sub(r'(?<!x)abc', r'def', string)