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

metaperl.com metaperl at gmail.com
Thu Sep 18 16:08:59 EDT 2008


On Sep 9, 9:23 am, s... at pobox.com wrote:
>     >> I really dont care if theexpressionis 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?
>
> Check Ka-Ping Yee's rxb module:
>
>    http://lfw.org/python/

Ok <http://lfw.org/python/rxb.py>
suffers from the possibility of putting shorter match before longer
one:

def either(*alternatives):
    options = []
    for option in alternatives:
        options.append(makepat(option).regex)
    return Pattern('\(' + string.join(options, '|') + '\)')


> Also, check PyPI to see if
> someone has already updated rxb for use with re.

No one has - http://pypi.python.org/pypi?%3Aaction=search&term=rxb&submit=search

no results returned






More information about the Python-list mailing list