Replacing word not contained in string wrapped in quotations (python)
Solution 1:
You don't need a regex. You can split the string on quotes and change the word in the even chunks (the odd chunks are within quotes):
text = '''Hello this is word, word is: "this part includes word!". Word can be anywhere in the quotes, "word", " ... word ...", etc...'''
out = '"'.join(s if i%2 else s.replace('word', 'new')
for i,s in enumerate(text.split('"')))
output:
'Hello this is new, new is: "this part includes word!". Word can be anywhere in the quotes, "word", " ... word ...", etc...'