splitting words with brackets

Simon Forman rogue_pedro at yahoo.com
Wed Jul 26 17:03:32 EDT 2006


Qiangning Hong wrote:
> 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']

What are the desired results in cases like this:

"(a b)[c d]" or "(a b)(c d)" ?




More information about the Python-list mailing list