splitting words with brackets

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed Jul 26 17:33:52 EDT 2006


Ah, I had just made the same change!


from pyparsing import *

wrd = Word(alphas)
parenList = "(" + SkipTo(")") + ")"
brackList = "[" + SkipTo("]") + "]"
listExpr = ZeroOrMore( Combine( OneOrMore( parenList | brackList | wrd ) ) )

t = "a (b c) d [e f g] h i(j k) l [m n o]p q r[s] (t u)v(w) (x)(y)z"
print listExpr.parseString(t)


Gives:
['a', '(b c)', 'd', '[e f g]', 'h', 'i(j k)', 'l', '[m n o]p', 'q', 'r[s]',
'(t u)v(w)', '(x)(y)z']






More information about the Python-list mailing list