0

I have a string which contains multiple types of brackets

strng="example - example [Bracket one] middle text to be retained (second bracket) example {third one}"

should become

"example - example middle text to be retained example"

So, I tried to use regular expressions but since I am using groups for different bracket types, the substitution works only if the brackets are side by side. As stated below:

pattern=re.compile(r'((\[|\().*(\]|\)))')
print(re.sub(pattern,"", strng))

How do I fix this problem?

  • Check this https://stackoverflow.com/questions/14596884/remove-text-between-and-in-python/14598135 – Alex Jul 30 '20 at 11:47
  • If you are open for none regex based solutions, here is one I wrote https://pastebin.com/JLJSDiz9 It might require some testing and tweaks I'll leave it up for you! – Aviel Fedida Jul 30 '20 at 11:55
  • https://www.kite.com/python/answers/how-to-remove-multiple-characters-from-a-string-in-python – dsanatomy Jul 30 '20 at 11:55

0 Answers0