Re: splitting words with brackets



Tim Chase wrote:
>>> import re
>>> s ='a (b c) d [e f g] h ia abcd(b c)xyz d [e f g] h i'
>>> r = re.compile(r'(?:\S*(?:\([^\)]*\)|\[[^\]]*\])\S*)|\S+')
>>> r.findall(s)
['a', '(b c)', 'd', '[e f g]', 'h', 'ia', 'abcd(b c)xyz', 'd',
'[e f g]', 'h', 'i']

[...]
However, the above monstrosity passes the tests I threw at
it.

but it can't pass this one: "(a c)b(c d) e"
the above regex gives out ['(a c)b(c', 'd)', 'e'], but the correct one
should be ['(a c)b(c d)', 'e']

.