I have a simple function to remove a word from some text:
def remove_word_from(word, text): if not text or not word: return text rec = re.compile(r'(^|\s)(' + word + ')($|\s)', re.IGNORECASE) return rec.sub(r'\1\3', text, 1)
The problem, of course, is that if a word contains characters like "(" or ")" things break, and it usually seems unsafe to stick with a random word in the middle of a regular expression.
What is the best practice for handling such cases? Is there a convenient, safe function that I can call to avoid a βwordβ so that it can be used safely?
python regex
Parand
source share