如何强制Python RegEx匹配由|连接的所有可能的组。 (*要么*)

时间:2019-06-13 02:51:44

标签: python regex

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

我已经尝试过:(?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

1 个答案:

答案 0 :(得分:0)

也许在这里,我们可以使用finditer并测试我们的表情:

Demo

测试

# 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}`);
    });
}