Bash-like brace expansion

Peter Waller peter.waller at gmail.com
Thu Mar 26 12:14:40 EDT 2009


On Mar 24, 5:39 pm, Peter Waller <peter.wal... at gmail.com> wrote:
> I've since heard that a 'better way' would be to use pyparsing. Also,
> I saw that python has dropped the idea of having recursive regular
> expressions at the moment.

I've written a pyparsing parser which is very much shorter and nicer
than the original parser. I have not yet figured out how to process
the result yet though, I'm having a hard time dealing with the
recursion in my brain. Here it is anyway, in case someone smarter than
me can figure out how to turn the output of this into the relevant
list of expansions.

from pyparsing import (Suppress, Optional, CharsNotIn, Forward,
Group,
                       ZeroOrMore, delimitedList)

lbrack, rbrack = map(Suppress, "{}")

optAnything = Group(Optional(CharsNotIn("{},")))



braceExpr = Forward()

listItem = optAnything ^ Group(braceExpr)

bracketedList = Group(lbrack + delimitedList(listItem) + rbrack)

braceExpr << optAnything + ZeroOrMore(bracketedList + optAnything)



result = braceExpr.parseString(text).asList()



More information about the Python-list mailing list