[英]Regular expression match a or b pattern
我在python中使用正则表达式库遇到了一个小问题,特别是使用具有不同模式的match方法:
import re
files = ["noi100k_0p55m0p3_fow71f",\
"fnoi100v5_71f60s",\
"noi100k_0p55m0p3_151f_560s",\
"noi110v25_560s"]
for i in files:
keyws = i.split("_")
for j in keyws:
if re.match(r"noi(\w+)k|fnoi(\w+)v(\w+)|noi(\w+)v(\w+)",j):
print "Results :", re.match(r"noi(\w+)k|fnoi(\w+)v(\w+)|noi(\w+)v(\w+)",j).group(1)
结果是:
Results : 100
Results : None
Results : 100
Results : None
当我期望:
Results : 100
Results : 100
Results : 100
Results : 110
唯一的匹配是"noi(\\w+)k"
它似乎没有测试其他模式,但是re.match(a|b,string)
应该检查a
和b
模式吗?
您的组从左到右编号; 如果选择之一相匹配,那么你需要提取该组 。
您有5个组,第1组,第2和3组,或者第4和5组将包含一个匹配项:
for j in keyws:
match = re.match(r"noi(\w+)k|fnoi(\w+)v(\w+)|noi(\w+)v(\w+)",j)
if match:
results = match.group(1) or match.group(2) or match.group(4)
print "Results :", results
将在每个替代方案中打印第一个匹配的\\w+
组。
演示:
>>> import re
>>> files = ["noi100k_0p55m0p3_fow71f",\
... "fnoi100v5_71f60s",\
... "noi100k_0p55m0p3_151f_560s",\
... "noi110v25_560s"]
>>> for i in files:
... keyws = i.split("_")
... for j in keyws:
... match = re.match(r"noi(\w+)k|fnoi(\w+)v(\w+)|noi(\w+)v(\w+)",j)
... if match:
... results = match.group(1) or match.group(2) or match.group(4)
... print "Results :", results
...
Results : 100
Results : 100
Results : 100
Results : 110
如果您不打算使用其他两个捕获的(\\w+)
组,请删除括号以使选择匹配的组更加容易:
match = re.match(r"noi(\w+)k|fnoi(\w+)v\w+|noi(\w+)v\w+",j)
if match:
results = next(g for g in match.groups() if g)
print "Results :", results
选择第一个不为空的匹配组。
如果您也接受fnoi(\\w+)k
,则可以进一步简化您的模式:
match = re.match(r"f?noi(\w+)[kv](\w*)", j)
此时只有一个.group(1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.