我正在尝试使用正则表达式替换一些内容。我可以使用以下方法做到这一点
EX: """ The search technique is usefull, the search technique is usefull """
old = 'the'
toreplace = "<span class='highlight' STYLE='background-color:yellow'>" + old + "</span>"
pattern = re.compile(re.escape(old), re.I)
highlighted_txt = re.sub(pattern,toreplace,A,count)
" <span class='highlight' STYLE='background-color:yellow'>the</span> search tech
nique is usefull, <span class='highlight' STYLE='background-color:yellow'>the</s
pan> search technique is usefull "
但我想做的是用在内容中准确找到的东西替换旧词,比如第二个“The”应该替换为
<span class='highlight' STYLE='background-color:yellow'>The</span>
" <span class='highlight' STYLE='background-color:yellow'>the</span> search tech
nique is usefull, <span class='highlight' STYLE='background-color:yellow'>The</s
pan> search technique is usefull "