I am looking for a way to replace a word, however only when its not surounded by quotations.
For example
Replacing Hello
with Hi
Regular expressions are awesome:
>>>import re
>>>expression = re.compile("(?!(\"|'))Hello(?!(\"|'))")
>>>expression.sub("Hi",'This string says "Hello" and Hello')
This string says "Hello" and Hi
The only problem with that is that it will also fail to replace "Hello and Hello", if that becomes an issue you can add specific cases for them.