简体   繁体   English

Python 正则表达式 re.finditer 2 匹配

[英]Python Regular Expression re.finditer 2 matches

Im looking to use single function to match for multiple values that can be used within another function.我希望使用单个函数来匹配可在另一个函数中使用的多个值。

I can get below to work with single regex value, looking for advise to match on second regex "regex2"我可以在下面使用单个正则表达式值,寻找与第二个正则表达式“regex2”匹配的建议

Working ---在职的 - -

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

Proposed --- trying to find match for both "Created on" and "Copied On"建议---尝试为“创建于”和“复制于”找到匹配项

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

I can get both regexs to work as single functions我可以让两个正则表达式作为单个函数工作

To see why this approach won't work, try to execute in the interpreter 1 or 2 .要了解为什么这种方法不起作用,请尝试在解释器1 or 2中执行。 This behavior is explained here .此行为在此处进行了解释。

I would search both patterns individually, and then go over them in two subsequent for loops.我会分别搜索这两个模式,然后在两个后续for循环中检查它们。 If you need one single iterator object, it should be possible to use如果您需要一个迭代器对象,应该可以使用

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

to chain both iterator objects together.将两个迭代器对象链接在一起。

Combine the two regular expressions with an |将两个正则表达式与|结合起来(or). (要么)。 Now there will be 4 groups returned for each match, two of which will be None depending upon what was matched.现在每场比赛将返回 4 个组,其中两个将是None ,具体取决于匹配的内容。 Even though you had a for loop, you were issuing a return after retrieving the first match, and that was not correct.即使你有一个for循环,你在检索到第一个匹配项后发出return ,这是不正确的。 The updated code, which uses a list comprehension to return all the matches:更新后的代码,它使用列表理解来返回所有匹配项:

import re重新进口

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 \nIt was Copied on Friday for Sally.'))

Prints:印刷:

[('Tuesday', 'Mary', None, None), (None, None, 'It was ', 'Friday')]
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 '', ''

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

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