regular expression questions using module re - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: regular expression questions using module re (/thread-21943.html) |
regular expression questions using module re - Skaperen - Oct-22-2019 1. i need to match 2 possible forms of a file name. what i did was put one form on each side of a vertical bar ('|'). what i am wondering is how to deal with groups because one form has all 3 groups and the other form has only 2 groups. do i need to have the same number of groups on each side of the vertical bar? or can i use '()' to include a dummy group? or will the .groups() method just return a 3-tuple in one case and a 2-tuple if the other matches? 2. in my expression is a match group for a particular pattern, but if the string has more than one such pattern i want to match the last one and get whatever that pattern is in the string when calling .groups(). how can i be sure of that? RE: regular expression questions using module re - Gribouillis - Oct-22-2019 For question 1, the match object will always have 5 groups, for example >>> import re >>> p = re.compile(r'(f)(o)(o)|(b)(a)r') >>> match = p.search('This is a foo thing') >>> match.groups() ('f', 'o', 'o', None, None) >>> match = p.search('This is a bar thing') >>> match.groups() (None, None, None, 'b', 'a')For question 2, I don't understand the question. An example would help. RE: regular expression questions using module re - Skaperen - Oct-22-2019 if regexp is 'foo(.+)bar' and my string is 'foo123bar...foo789bar' i want to get '789' .
RE: regular expression questions using module re - Gribouillis - Oct-22-2019 You could use more_itertools.last() >>> import re >>> from more_itertools import last >>> p = re.compile(r'foo(.+?)bar') # <--- note the ? for the non greedy version of + >>> s = 'foo123bar...foo789bar' >>> match = last(p.finditer(s)) >>> match.group(1) '789' RE: regular expression questions using module re - Skaperen - Oct-22-2019 given the answer to #1 being that it returns 5 groups, and filling in what is matched, then #2 is meaningless. |