0

I have a paragraph of text and a list of substrings from that text which I want to replace with a different substring.

newstring = "foo"
re_word = r'{0}'.format(word)
text = re.sub(re_word, newstring, text)

I'm doing this with a number of different 'paragraphs' of text. For some paragraphs it works fine, but for others I get the following error from the final line:

sre_constants.error: missing ), unterminated subpattern at position 10

I suspect that the string I'm looking for contains a character that confuses Regex, ie. I need to escape it. How might I get around that?

mosh.jinton
  • 188
  • 11

0 Answers0