2012-12-31 160 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