python multiple regular expressions

♀尐吖头ヾ 提交于 2019-12-21 20:55:33

问题


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


来源:https://stackoverflow.com/questions/14100868/python-multiple-regular-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!