Python Forum
regular expression questions using module re
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regular expression questions using module re
#1
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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
if regexp is 'foo(.+)bar' and my string is 'foo123bar...foo789bar' i want to get '789'.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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'
Reply
#5
given the answer to #1 being that it returns 5 groups, and filling in what is matched, then #2 is meaningless.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 321 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Regular Expression search to comment lines of code Gman2233 5 1,653 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,654 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,914 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,160 Mar-09-2022, 10:34 PM
Last Post: snippsat
  regular expression question Skaperen 4 2,469 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How can I find all combinations with a regular expression? AlekseyPython 0 1,660 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 2,905 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,406 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Regular expression: return string, not list Pavel_47 3 2,486 Jan-14-2021, 11:49 AM
Last Post: Pavel_47

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020