creating an (inefficent) alternating regular expression from a list of options
fredrik at pythonware.com
Tue Sep 9 18:42:28 CEST 2008
Larry Bates wrote:
>> vowel_regexp = oneOf("a aa i ii u uu".split()) # yielding r'(aa|a|uu|
>> 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
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.
More information about the Python-list