python多个正则表达式 [英] python multiple regular expressions

查看:237
本文介绍了python多个正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  regex = re.compile (...)
regex2 = re.compile(...)
regex3 = re.compile(...)
regex4 = re.compile( ...)
如果regex.match(string)== None和regex2.match(string)== None和regex3.match(string)== None和regex4.match(string)== None:

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

解决方案

  r_list = [re.compile (...),
re.compile(...),
re.compile(...),
re.compile(... )]
如果有的话(r_list中的r的r.match(string)):
#如果至少有一个正则表达式的匹配做了smth
pre>

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'?

解决方案

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

这篇关于python多个正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆