Regexp parser and generator

George Sakkis george.sakkis at
Tue Nov 4 20:34:52 CET 2008

Is there any package that parses regular expressions and returns an
AST ? Something like:

>>> parse_rx(r'i (love|hate) h(is|er) (cat|dog)s?\s*!+')
Regex('i ', Or('love', 'hate'), ' h', Or('is', 'er'), ' ', Or('cat',
'dog'), Optional('s'), ZeroOrMore(r'\s'), OneOrMore('!'))

Given such a structure, I want to create a generator that can generate
all strings matched by this regexp. Obviously if the regexp contains a
'*' or '+' the generator is infinite, and although it can be
artificially constrained by, say, a maxdepth parameter, for now I'm
interested in finite regexps only. It shouldn't be too hard to write
one from scratch but just in case someone has already done it, so much
the better.


More information about the Python-list mailing list