Regexp split string with additional conditions

I have string like

a = 'text1, text2 (subtext1, subtext2), text3'

Need split that string on parts by comma-symbols, but only those that are not in the frame brackets:

splited = ['text1', 'text2 (subtext1, subtext2)', 'text3']

How make that with regular expression?

Upvotes: 0

Views: 114

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Use a negative lookahead assertion based regex.

>>> a = 'text1, text2 (subtext1, subtext2), text3'
>>> re.split(r',(?![^()]*\))', a)
['text1', ' text2 (subtext1, subtext2)', ' text3']
>>> re.split(r',\s*(?![^()]*\))', a)
['text1', 'text2 (subtext1, subtext2)', 'text3']

DEMO

OR

Positive lookahead based regex.

>>> re.split(r',\s*(?=[^()]*(?:\(|$))', a)
['text1', 'text2 (subtext1, subtext2)', 'text3']

Upvotes: 2

Related Questions