0

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

DeclanG
  • 240
  • 2
  • 6
  • 21

3 Answers3

1

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.

sammy
  • 857
  • 5
  • 13
  • so in my case y_iter = chain(regex, regex2) , I could then call y_iter. trying to figure out how to relate to above single function. – DeclanG Dec 01 '19 at 20:06
  • def parse_description(description): regex = r"^Created on\((.*?)\) for (.*?) " regex2 = r"^(.*?)Copied on (.*?) " matches = re.finditer(regex, description, re.MULTILINE) matches2 = re.finditer(regex2, description, re.MULTILINE) for matchNum, match in enumerate(matches): return match.groups() return '', '' for matchNum, match in enumerate(matches2): return match.groups() return '', '' – DeclanG Dec 01 '19 at 20:11
  • I was able to get it working thanks to your comment and link https://stackoverflow.com/questions/44116557/how-to-extend-concatenate-two-iterators-in-python/44116611 – DeclanG Dec 13 '19 at 01:29
0

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')]
Booboo
  • 38,656
  • 3
  • 37
  • 60
  • now getting error , when trying this function. "ValueError: not enough values to unpack (expected 2, got 1)" @Booboo – DeclanG Dec 12 '19 at 23:58
  • What was the input text that caused the error and specifically what line of code threw the error? – Booboo Dec 13 '19 at 03:50
  • was able to get it working using from itertools import chain y_iter = chain(l1, l2) – DeclanG Dec 13 '19 at 23:07
0
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 '', ''
DeclanG
  • 240
  • 2
  • 6
  • 21