我正在检查一串文字,我希望更改文本中的某些单词,忽略引号内的任何内容。
示例:如果我想替换"我的"用" MY"
Hello my name is "Tom, and my favourite sport is football"
会变成
Hello MY name is "Tom, and my favourite sport is football"
我正在使用正则表达式来搜索要替换的单词。
我用Python编写这个脚本。
编辑:我将在原始文本中搜索列表中的单词,而不是文字单词。匹配整个单词'。
也很重要答案 0 :(得分:0)
您可以使用此正则表达式:
(\bmy\b)(?=(?:[^"]|"[^"]*")*$)
Python演示:
>>> 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