splitting words with brackets
Qiangning Hong
hongqn at gmail.com
Wed Jul 26 16:42:48 EDT 2006
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']
More information about the Python-list
mailing list