I am looking for a way to replace a word, however only when its not surounded by quotations.
For example Replacing Hello with Hi
Hello
Hi
This works for your test case.
import re foo = "Hello 'Hello' Nothing" mt = re.search(r"[^']Hello(\s+.*)", foo) if mt: foo = 'Hi' + match.group(1)