有没有办法在python中将列表中存储的正则表达式模式列表应用到单个字符串中?

DEz*_*zra 4 python regex list

我有一个我想要应用于字符串的正则表达式模式列表(存储在列表类型中).

有谁知道一个好方法:

  1. 将列表中的每个正则表达式模式应用于字符串和
  2. 如果匹配,则调用与列表中该模式关联的其他函数.

如果可能的话我想在python中这样做

提前致谢.

Eli*_*ght 10

import re

def func1(s):
    print s, "is a nice string"

def func2(s):
    print s, "is a bad string"

funcs = {
    r".*pat1.*": func1,
    r".*pat2.*": func2
}
s = "Some string with both pat1 and pat2"

for pat, func in funcs.items():
    if re.search(pat, s):
        func(s)
Run Code Online (Sandbox Code Playgroud)

上面的代码将调用字符串的两个函数,s因为两个模式都匹配.