关于regex:Python正则表达式re.finditer 2匹配项

Python Regular Expression re.finditer 2 matches

我希望使用单个函数来匹配可在另一个函数中使用的多个值。

我可以从下面开始使用单个正则表达式值,寻找与第二个正则表达式" regex2 "匹配的建议

工作中---

1
2
3
4
5
6
def parse_desc(description):
    regex = r"^Created on\\((.*?)\\) for (.*?)"
    matches = re.finditer(regex, description, re.MULTILINE)
    for matchNum, match in enumerate(matches):
        return match.groups()
    return '', ''

建议--试图找到" Created on "和" Copyed On "的匹配项。

1
2
3
4
5
6
7
def pass_desc(description):
    regex = r"^Created on\\((.*?)\\) for (.*?)"
    regex2 = r"^(.*?)Copied on (.*?)"
    matches = re.finditer(regex, description, re.MULTILINE) or re.finditer(regex2, description, re.MULTILINE)
    for matchNum, match in enumerate(matches):
        return match.groups()
    return '', ''

我可以让两个正则表达式都可以作为单个函数工作


要了解为什么这种方法行不通,请尝试在解释器1 or 2中执行。此处说明了此行为。

我将分别搜索这两种模式,然后在随后的两个for循环中遍历它们。如果只需要一个迭代器对象,则应该可以使用

1
2
from itertools import chain
y_iter = chain(l1, l2)

将两个迭代器对象链接在一起。


1
2
3
4
5
6
7
8
9
10
11
12
def pass_desc(description):
    regex = r"^Created on\\((.*?)\\) for (.*?)"
    regex2 = r"^(.*?)Copied on (.*?)"
    matches = re.finditer(regex, description, re.MULTILINE)
    matches2 = re.finditer(regex2, description, re.MULTILINE)

    from itertools import chain
    y_iter = chain(matches, matches2)

    for matchNum, match in enumerate(y_iter):
        return match.groups()
    return '', ''

|(或)组合两个正则表达式。现在,每个匹配项将返回4个组,其中两个将是None,具体取决于匹配的项。即使您有一个for循环,您在检索第一个匹配项后仍发出了return,这是不正确的。更新的代码,使用列表推导返回所有匹配项:

重新导入

1
2
3
4
5
6
def pass_desc(description):
    regex12 = r"^Created on\\((.*?)\\) for (.*?) |^(.*?)Copied on (.*?)"
    return [match.groups() for match in re.finditer(regex12, description, re.MULTILINE)]

print(pass_desc('Created on(Tuesday) for Mary \
It was Copied on Friday for Sally.'
))

打印:

1
[('Tuesday', 'Mary', None, None), (None, None, 'It was ', 'Friday')]