python - 如何将字符串与多个正则表达式匹配?

标签 python regex string

目前我正在使用下面的过滤器来增加 arr 中的元素,给定一个字符串列表作为参数,在 python 中是否有一种有效的方法来执行此操作。我有数百万个这样的列表来验证。

  def countbycat(tempfilter):
        arr=[0,0,0,0,0]
        apattern,dpattern,mpattern,upattern,tpattern = re.compile("^[a]--*"),re.compile("^[d]--*"),re.compile("^[m]--*"),re.compile("^[u]--*"),re.compile("^[t]--*")
        for each in tempfilter:
            if upattern.match(each):
                 arr[0]+=1
            elif mpattern.match(each):
                 arr[1]+=1
            elif dpattern.match(each):
                 arr[2]=1
            elif apattern.match(each):
                 arr[3]+=1
            elif tpattern.match(each):
                 arr[4]+=1
        return arr  

最佳答案

对于问题中给出的正则表达式,您可以使用以下使用字符类的正则表达式:

[admut]-
  • [admut] 将匹配 admu, t
  • ^ 可以省略,因为 re.match 只匹配字符串的开头。
  • 删除了 -* 因为它毫无意义;只有一个 - 足以检查 - 出现在 a/d/m/u/t 之后。

而不是使用数组,你可以使用字典;无需记住索引:

def countbycat(tempfilter):
    count = dict.fromkeys('admut', 0)
    pattern = re.compile("[admut]-")
    for each in tempfilter:
        if pattern.match(each):
            count[each[0]] += 1
    return count

而不是 dict.fromkeys , 你可以使用 collections.Counter .

关于python - 如何将字符串与多个正则表达式匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38161309/

相关文章:

string - 在 Bash 中,如何检查字符串是否以某个值开头?

python - 从字符串中删除大量前导空格和换行符

python - opencv 3.0 beta 创建新模块(可在 Python 中使用)

python - 我需要在 python 中运行 TCL 脚本,我的 TCL 脚本还有一个用户定义的(内部)包

javascript - 全局忽略某些字符

python - 将从文件读取的 True/False 值转换为 boolean 值

python - 克服 python 中的 appengine 500 字节字符串限制?考虑文本

python - C++ 嵌入式解释器和对象

java正则表达式拆分字符串

regex - Sublime Text 中使用 RegEx 进行高级查找和替换