-->

python multiple regular expressions

2020-07-23 05:58发布

问题:

I need to apply multiple regular expressions on a string which I'm doing like this:

regex = re.compile("...")
regex2 = re.compile("...")
regex3 = re.compile("...")
regex4 = re.compile("...")
if regex.match(string) == None and regex2.match(string) == None and regex3.match(string) == None and regex4.match(string) == None:

I was wondering if there is another way to somehow merge or combine the single regular expressions or if I'm already doing it the 'right way'?

回答1:

r_list = [re.compile("..."),
          re.compile("..."),
          re.compile("..."), 
          re.compile("...")]
if any(r.match(string) for r in r_list):
    # if at least one of the regex's matches do smth