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 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 '', '' |
用
重新导入
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')] |