Regexp parser and generator

George Sakkis george.sakkis at gmail.com
Wed Nov 5 21:52:30 EST 2008


On Nov 4, 9:56 pm, Paul McGuire <pt... at austin.rr.com> wrote:
> On Nov 4, 1:34 pm, George Sakkis <george.sak... at gmail.com> wrote:
>
>
>
> > 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.
>
> > George
>
> Check out this pyparsing regex inverter:http://pyparsing.wikispaces.com/file/view/invRegex.py
>

Neat, seems like a good excuse to look into pyparsing :)

Best,
George



More information about the Python-list mailing list