I am looking for a way to replace a word, however only when its not surounded by quotations.
For example
Replacing Hello
with Hi
Using regular expressions:
>>> import re
>>> re.sub(r'([^"\']|^)Hello([^"\']|$)', r'\1Hi\2', "Hello mate")
'Hi mate'
>>> re.sub(r'([^"\']|^)Hello([^"\']|$)', r'\1Hi\2', "'Hello' mate")
"'Hello' mate"
'([^"\']|^)Hello([^"\']|$)'
means 'The string Hello surrounded by something different than a single or double quote, or at the beginning or end of the line'.