I am trying to replace some content using regular expression. I am able to do that using the following method:
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 "
But what I want to do is replace the old word with what is found exactly in the content. Like the second "The" should be replaced with
<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 "
This fixed my problem