splitting words with brackets

Tim Chase python.list at tim.thechases.com
Wed Jul 26 22:11:28 EDT 2006


>> >>> r = re.compile(r'(?:\([^\)]*\)|\[[^\]]*\]|\S)+')
>> >>> r.findall(s)
>>['(a c)b(c d)', 'e']
> 
> Ah, it's exactly what I want!  I thought the left and right 
> sides of "|" are equal, but it is not true.

In theory, they *should* be equal. I was baffled by the nonparity
of the situation.  You *should" be able to swap the two sides of 
the "|" and have it treated the same.  Yet, when I tried it with 
the above regexp, putting the \S first, it seemed to choke and 
give different results.  I'd love to know why.

> Thank you and Simon for your kindly help!

My pleasure.  A nice diversion from swatting spammers and getting 
our network back up and running today.  I had hoped to actually 
get something productive done (i.e. writing some python code) 
rather than putting out fires.  Sigh.

-tkc









More information about the Python-list mailing list