0

I am currently learning regex in python and got stuck at a problem in which I have to separate the string such that there are words that are not between any ( ) or [ ].

Let me show an example:

String: Ann Arbor (University of Michigan)[1]

Output: Ann Arbor

Param Bedi
  • 13
  • 3
  • please let us know how you attempted to solve the problem (simple working code example) – Ophir Carmi Jul 18 '20 at 19:31
  • Yes, I tried. Being a noob in the regex, I was unable to solve the problem. I am practicing to get better at it. I am stuck at this problem for a few hours that is why I decided to look for the solution online but couldn't find any. – Param Bedi Jul 19 '20 at 10:09
  • I actually solved the problem without regex But the solution requires specifically a regex expression. That is why I am stuck. – Param Bedi Jul 19 '20 at 10:10

1 Answers1

0

This approach uses re.sub to remove anything in brackets and the brackets that surround them.

import re

string = 'Ann Arbor (University of Michigan)[1]'
print(re.sub("[\(\[].*?[\)\]]", "", string))
Harben
  • 1,430
  • 1
  • 9
  • 16