首页 > 解决方案 > 如何强制 Python RegEx 匹配由 | 连接的所有可能组 (*或者*)

问题描述

我是 RegEx 的新手,我想知道是否有一种方法可以强制 RegEx 在同一个“匹配”中匹配所有可能的组(如果有多个组),其中模式通过OR连接(见下文)。

我试过这个:(?P<broad>travel)|(?P<step>step)|(?P<dist>distance|far|km),但如果输入是:Tell me how many steps I traveled,代码只匹配旅行或步骤之一。我也尝试过使用findall而不是search,但是组信息丢失了(因为输出是一个列表)。

我希望代码可以匹配同一“匹配”中的所有可能组(如果可用),而不是在找到匹配项后立即退出。

电流输出:

Match 1
broad   None
step    step
dist    None
Match 2
broad   travel
step    None
dist    None

预期输出:

Match 1
broad   travel
step    step
dist    None

标签: pythonregex

解决方案


也许在这里,我们可以使用finditer和测试我们的表达式:

演示

测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(travel)|(step)|(distance|far|km)"

test_str = "Tell me how many steps I traveled"

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

演示

const regex = /(travel)|(step)|(distance|far|km)/gm;
const str = `Tell me how many steps I traveled`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}


推荐阅读