Home > database >  Extract contents within outer paranthesis
Extract contents within outer paranthesis

Time:12-06

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)']

CodePudding user response:

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

  • Related