In Python, I am trying to do
text = re.sub(r'\b%s\b' % word, "replace_text", text)
to replace a word with some text. Using re
rather than just doing text.replace
to replace only if the whole word matches using \b
. Problem comes when there are characters like +, (, [ etc
in word. For example +91xxxxxxxx
.
Regex treats this +
as wildcard for one or more and breaks with error. sre_constants.error: nothing to repeat
. Same is in the case of (
too.
Could find a fix for this after searching around a bit. Is there a way?