python splitting by 'and' and 'or', but not in parentheses

Viewed 191

I have the following string:

(some text) or ((other text) and (some more text)) and (still more text)

I would like a python regular expression that splits it up into

['(some text)', '((other text) and (some more text))', '(still more text)']

I've tried this but it doesn't work:

haystack = "(some text) or ((other text) and (some more text)) and (still more text)"
re.split('(or|and)(?![^(]*.\))', haystack) # no worky

Any help is appreciated.

4 Answers
Related