简体   繁体   English

正则表达式匹配a或b模式

[英]Regular expression match a or b pattern

I've got a small problem with the regular expression library in python, specifically with the match method with different patterns: 我在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)

The results are: 结果是:

Results : 100
Results : None
Results : 100
Results : None

When I would expect: 当我期望:

Results : 100
Results : 100
Results : 100
Results : 110

The only match is for "noi(\\w+)k" it does not seems to test the other patterns but re.match(a|b,string) should check the a and b pattern no? 唯一的匹配是"noi(\\w+)k"它似乎没有测试其他模式,但是re.match(a|b,string)应该检查ab模式吗?

Your groups are numbered from left to right; 您的组从左到右编号; if one of the alternatives matches, then it is that group you need to extract. 如果选择之一相匹配,那么你需要提取该组

You have 5 groups, either group 1, or groups 2 and 3, or groups 4 and 5 will contain a match: 您有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

would print the first matched \\w+ group in each alternative. 将在每个替代方案中打印第一个匹配的\\w+组。

Demo: 演示:

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

If you are not going to use of the other two captured (\\w+) groups, remove the parenthesis to make picking the matched group a little easier: 如果您不打算使用其他两个捕获的(\\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

which picks the first matched group that is not empty. 选择第一个不为空的匹配组。

Your pattern could be further simplified if you accept fnoi(\\w+)k as a possibility too: 如果您也接受fnoi(\\w+)k ,则可以进一步简化您的模式:

match = re.match(r"f?noi(\w+)[kv](\w*)", j)

at which point there is only ever a .group(1) . 此时只有一个.group(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM