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

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Sep 9 15:39:20 CEST 2008


On Tue, 09 Sep 2008 08:19:04 -0500, Larry Bates wrote:

>> 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())

I'd throw `re.escape()` into that function just in case `s` contains 
something with special meaning in regular expressions.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list