0

I am trying to understand why the following use of regular expressions does not work, i.e. the pattern is not found in the string (general expression of re.sub is re.sub(pattern, repl, string). Can you please tell me why and, if possible, explain how could I have altered the code to make it work?

import re

string = " Many of them are not just serving time in prison, but they are being let out of prison and back into our communities, having committed appalling crimes. They are not being kicked out.</p><p><i>[</i></p><p><i>Interruption.</i></p><p><i>]</i></p><p>And no doubt they are indeed receiving benefits. That is why the British people are fed up and want action to be taken. It is unlikely that my hon."

testOutput = re.sub(r'</p><p><i>[</i></p><p><i>Interruption.</i></p><p><i>]</i></p><p>',' ',string)

print(testOutput)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Stas003
  • 31
  • 4

1 Answers1

0

I actually found an answer. the brackets have their own meaning in re.sub so I had to substitute '[' with 'backslash['

That is done to convert [ into symbol and match pattern with section of the string

Stas003
  • 31
  • 4