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

Larry Bates larry.bates at vitalEsafe.com
Tue Sep 9 15:19:04 CEST 2008


metaperl.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?
> 
> 

Perhaps I'm missing something but your function call oneOf(...) is longer than 
than actually specifying the result.

You can certainly write quite easily:

def oneOf(s):
     return "|".join(s.split())

-Larry



More information about the Python-list mailing list