1

I am examining a string of text, and I am looking to change certain words in the text, ignoring anything inside quotation marks.

Example: If I wanted to replace "my" with "MY"

Hello my name is "Tom, and my favourite sport is football"

Would become

Hello MY name is "Tom, and my favourite sport is football"

I am using regex to search for the words to be replaced.

I'm writing this script in Python.

Edit: I will be searching the original text for words from a list, not literal words. It is also important to match 'whole words'.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user2070229
  • 33
  • 1
  • 6

1 Answers1

0

You can use this regex:

(\bmy\b)(?=(?:[^"]|"[^"]*")*$)

Demo

Python demo:

>>> txt='''\
... Hello my name is "Tom, and my favourite sport is football" my O my
... Hello Tom, my name is Bonney
... not mymymy'''
>>> tgt='my'
>>> print re.sub(r'(\b%s\b)(?=(?:[^"]|"[^"]*")*$)' % tgt, tgt.upper(), txt)
Hello MY name is "Tom, and my favourite sport is football" MY O MY
Hello Tom, MY name is Bonney
not mymymy
dawg
  • 98,345
  • 23
  • 131
  • 206