I am looking for a way to replace a word, however only when its not surounded by quotations.
For example
Replacing Hello
with Hi
Consider using a regular expression (not the only way, but I'd go with that).
In [2]: print s
Hello 'Hello' Nothing
In [3]: import re
In [4]: re.sub("(?<!')Hello(?!')", 'Hi', s)
Out[4]: "Hi 'Hello' Nothing"
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).
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.
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)
Use the substring function find all the occurances of the word you want to replace, for each word look at one index previous to what the substring function returns and see if its a quote.
eg. ""Hello 'Hello' Nothing"
Substring function returns 0 -- so of course there is no quote Substring function returns 6 -- check string[5] -- theres a quote, look for next ocurance
How can you keep checking using the substring function? something like this:
startindex=0
while(!done):
index=substr(string, startindex)
if(str[index-1] == "'")
startindex=index
continue
from here you'll figure it out
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'.