0

I have the following regular expression, and i need some advice about it. I need advice how can i make to highlight the text without changing the word form (uppercase to stay uppercase). I have a list of word that i like to highlight, so i got the following:

  def tagText(self,listSearch,docText):  
    docText=docText.decode('utf-8') 

    for value in listSearch: 
       replace = re.compile(ur""+value+"",  flags=re.IGNORECASE | re.UNICODE)  
       docText = replace.sub(u"""<b style="color:red">"""+value+"""</b>""", docText,  re.IGNORECASE | re.UNICODE)

    return docText
Levon
  • 138,105
  • 33
  • 200
  • 191
badc0re
  • 3,333
  • 6
  • 30
  • 46

1 Answers1

2

You need to use placeholders in your replacement string, instead of the literal value.

def tag_text(self, items, text):
    text = text.decode('utf-8') 
    for item in items: 
        text = re.sub(
            re.escape(item), 
            ur'<b style="color:red">\g<0></b>', 
            text,
            flags=re.IGNORECASE | re.UNICODE)
    return text

print tag_text(None, ["foo", "bar"], "text with Foo and BAR")
# text with <b style="color:red">Foo</b> and <b style="color:red">BAR</b>

(I also cleaned up your function a bit to make it look more "pythonic").

georg
  • 211,518
  • 52
  • 313
  • 390