3

我需要在我这样做的字符串上应用多个正则表达式:

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:

我想知道是否有另一种方法可以以某种方式合并或组合单个正则表达式,或者我是否已经在以“正确的方式”进行操作?

4

1 回答 1

3
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
于 2012-12-31T11:28:22.560 回答