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

metaperl.com metaperl at gmail.com
Thu Sep 18 22:20:58 CEST 2008


On Sep 9, 12:42 pm, Fredrik Lundh <fred... at pythonware.com> wrote:

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

yes, thank you very much:

import re

def oneOf(s):
    alts = sorted(s.split(), reverse=True)
    alts = [re.escape(s) for s in alts]
    return "|".join(alts)




More information about the Python-list mailing list