splitting words with brackets

Tim Chase python.list at tim.thechases.com
Wed Jul 26 17:05:30 EDT 2006


> 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']

Ah...the picture is becoming a little more clear:

 >>> r = re.compile(r'(?:\([^\)]*\)|\[[^\]]*\]|\S)+')
 >>> r.findall(s)
['(a c)b(c d)', 'e']

It also works on my original test data, and is a cleaner regexp 
than the original.

The clearer the problem, the clearer the answer. :)

-tkc







More information about the Python-list mailing list