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

Fredrik Lundh fredrik at pythonware.com
Tue Sep 9 12:42:28 EDT 2008


Larry Bates wrote:

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

"|" works strictly from left to right, so that doesn't quite work since 
it doesn't place "aa" before "a".  a simple reverse sort will take care 
of that:

     return "|".join(sorted(s.split(), reverse=True))

you may also want to do re.escape on all the words, to avoid surprises 
when the choices contain special characters.

</F>




More information about the Python-list mailing list