Extract contents within outer paranthesis

Question:

I am trying to extract all contents within outer paranthesis even if inner paranthesis is present. I used the following code

import re
s = "Text,Hei(Conductor (Opera), Conductor (Symphonic))"
l= re.findall(r'((.*?))',s)

This gives ['Conductor (Opera', 'Symphonic'] But I need ['Conductor (Opera)', 'Conductor (Symphonic)']

Asked By: imhans4305

||

Answers:

Try:

import re
s = "Text,Hei(Conductor (Opera), Conductor (Symphonic))"
l= re.findall(r'[( ]([A-Z][^)]*))',s)

>>> l
['Conductor (Opera)', 'Conductor (Symphonic)']

Regex 101 Demo

Answered By: dawg
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.