Regexp parser and generator

Paul McGuire ptmcg at austin.rr.com
Tue Nov 4 21:56:32 EST 2008


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

Here is what your example generates:
i (love|hate) h(is|er) (cat|dog)s?
Parse time: 0.17 seconds
16
i love his cat
i love his cats
i love his dog
i love his dogs
i love her cat
i love her cats
i love her dog
i love her dogs
i hate his cat
i hate his cats
i hate his dog
i hate his dogs
i hate her cat
i hate her cats
i hate her dog
i hate her dogs

-- Paul



More information about the Python-list mailing list