def remove_word_from(word, text):
    if not text or not word:
        return text
rec = re.compile('(^|\\s)(' + sanitize(word) + ')($|\\s)', re.IGNORECASE)
return rec.sub('\\1\\3', text, 1)
Python: Best practice for dynamically constructing regex