Cleb Cleb - 1 year ago 117
Python Question

How to remove all text between the outer parentheses in a string?

When I have a string like this:

s1 = 'stuff(remove_me)'

I can easily remove the parentheses and the text within using

# returns 'stuff'
res1 = re.sub(r'\([^)]*\)', '', s1)

as explained here.

But I sometimes encounter nested expressions like this:

s2 = 'stuff(remove(me))'

When I run the command from above, I end up with


I also tried:

re.sub('\(.*?\)', '', s2)

which gives me the same output.

How can I remove everything within the outer parentheses - including the parentheses themselves - so that I also end up with
(which should work for arbitrarily complex expressions)?

Answer Source

re matches are eager so they try to match as much text as possible, for the simple test case you mention just let the regex run:

>>> re.sub(r'\(.*\)', '', 'stuff(remove(me))')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download