简体   繁体   中英

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"

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 . This behavior is explained here .

I would search both patterns individually, and then go over them in two subsequent for loops. 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. Even though you had a for loop, you were issuing a return after retrieving the first match, and that was not correct. 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 '', ''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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