我有一个我想要应用于字符串的正则表达式模式列表(存储在列表类型中).
有谁知道一个好方法:
如果可能的话我想在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
因为两个模式都匹配.