I am looking for a way to replace a word, however only when its not surounded by quotations.
For example
Replacing Hello with Hi
Try this:
import re
def callback(match):
rep = 'Hi'
return match.group(1)+rep+match.group(2)
your_string = "Hello 'Hello' Nothing"
print re.sub("([^\']|^)Hello([^\']|$)", callback, your_string)
This will match the word Hello that is surrounded by anything except ' (^ in [] means anything but). I also added the |^ and |$ to match the word Hello that is in the end or the beginning of the string.
It will replace it by the first part in parenthesis along with Hi and the second part (whatever they are).