Reputation: 507
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
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']
OR
Positive lookahead based regex.
>>> re.split(r',\s*(?=[^()]*(?:\(|$))', a)
['text1', 'text2 (subtext1, subtext2)', 'text3']
Upvotes: 2