creating an (inefficent) alternating regular expression from a list of options

Nick Craig-Wood nick at craig-wood.com
Tue Sep 9 10:36:24 EDT 2008


metaperl.com <metaperl at gmail.com> wrote:
>  Pyparsing has a really nice feature that I want in PLY. I want to
>  specify a list of strings and have them converted to a regular
>  expression.
> 
>  A Perl module which does an aggressively optimizing job of this is
>  Regexp::List -
>  http://search.cpan.org/~dankogai/Regexp-Optimizer-0.15/lib/Regexp/List.pm
> 
>  I really dont care if the expression is optimal. So the goal is
>  something like:
> 
>  vowel_regexp = oneOf("a aa i ii u uu".split())  # yielding r'(aa|a|uu|
>  u|ii|i)'
> 
>  Is there a public module available for this purpose?

I wrote one of these in perl a while ago

  http://www.craig-wood.com/nick/pub/words-to-regexp.pl

It transforms the regular expression recursively into more efficient
ones.  It uses regular expressions to do that.  I considered porting
it to python but looking at the regular expressions made me feel weak
at the knees ;-)

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list