0

im trying to substitute html code with keywords in string

result2 = ['no one likes overpriced coffee but people love iced coffee']
mylist = ['words','to','substitute','iced coffee']
for word in mylist:
        compiled = re.compile(re.escape(word), re.IGNORECASE)
        result2 = compiled.sub(fr'<a href="{word_dict[word]}" style="color:#2c7593; font-weight:bold; text-shadow: 1px 1px #FFFFFF;">{word}</a>', result2,count=1)

i want only iced coffee to be substituted so result to be no one likes overpriced coffee but people love <a href="{word_dict[word]}" style="color:#2c7593; font-weight:bold; text-shadow: 1px 1px #FFFFFF;">iced coffee</a> but instead im getting no one likes overpr<a href="{word_dict[word]}" style="color:#2c7593; font-weight:bold; text-shadow: 1px 1px #FFFFFF;">iced coffee</a> but people love <a href="{word_dict[word]}" style="color:#2c7593; font-weight:bold; text-shadow: 1px 1px #FFFFFF;">iced coffee</a>

i found that i can use '\b' but i dont know where im supposed to put it. i tried putting it in front of f'\b{word}' in re.escape() and in front of replacement string and it doesnt even replace anymore.

barkhar
  • 1
  • 1

0 Answers0