Bottom Page

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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#3
if regexp is 'foo(.+)bar' and my string is 'foo123bar...foo789bar' i want to get '789'.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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'
Quote
#5
given the answer to #1 being that it returns 5 groups, and filling in what is matched, then #2 is meaningless.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pass results of expression to another expression cmdr_eggplant 2 236 Mar-26-2020, 06:59 AM
Last Post: ndc85430
  Hide symbol or characters in regular expression Gateux 0 143 Mar-21-2020, 10:25 AM
Last Post: Gateux
  Regular expression: match pattern at the end only Pavel_47 3 241 Nov-27-2019, 07:51 PM
Last Post: Gribouillis
  Substitution with regular expression returns hidden character SOH bajacri 2 451 Nov-17-2019, 03:38 AM
Last Post: bajacri
  regular expression and sub yokaso 6 364 Nov-03-2019, 11:45 AM
Last Post: yokaso
  Regular Expression rzbddm 4 858 Oct-30-2018, 04:25 PM
Last Post: stranac
  regular expression question Sanlus 6 850 Aug-04-2018, 06:49 PM
Last Post: volcano63
  regular expression for a transformation bluefrog 1 871 May-30-2018, 11:53 AM
Last Post: killerrex
  Regular expression to print maching anna 17 2,231 Apr-24-2018, 11:43 AM
Last Post: Gribouillis
  Regular Expression Help Dazzler 7 1,464 Apr-08-2018, 03:27 PM
Last Post: ljmetzger

Forum Jump:


Users browsing this thread: 1 Guest(s)