2012-12-31 96 views
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: 

我想知道是否有另一种方式以某种方式合并或合并的单正则表达式或如果我已经在做'正确的方式'?

+1

好,这将有助于如果你能给样品输入/输出,什么正则表达式的样子......但本质上,你可以正则表达式的表达式组合成几乎所有的时间一个正则表达式。通常使用'|'。 –

+0

你是否想要应用依赖于比赛的不同动作? – root

+0

例如我有两个表达式regex1 = re.compile(“[0-9] + [a-zA-z0-9] *”) \t regex2 = re.compile(“[a-zA-z] * [0-9 ] + [a-zA-z] + [a-zA-z0-9] *“),如果匹配的结果不是'无,则没有任何反应 – wasp256

回答

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