Context:
When running a regex match in Perl, $1
, $2
can be used as references to captured regex references from the match, similarly in Python \g<0>
,\g<1>
can be used
Perl also has a $
special reference which refers to the captured group with highest numerical value
My question:
Does Python have an equivalent of $
?
I tried \g< >
and tried looking in the documentation which only says:
There’s also a syntax for referring to named groups as defined by the
(?P<name>...)
syntax.\g<name>
will use the substring matched by the group namedname
, and\g<number>
uses the corresponding group number.\g<2>
is therefore equivalent to\2
, but isn’t ambiguous in a replacement string such as\g<2>0
. (\20
would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character'0'
.) The following substitutions are all equivalent, but use all three variations of the replacement string.
CodePudding user response:
>>> import re
>>> s = 'group2'
>>> m = re.search( r'(group1)|(group2)|(group3)', s )
>>> [ g for g in m.groups() if g is not None ]
['group2']
CodePudding user response:
Suppose you have an arbitrary length match (ie, for some reason you don't know how many capture groups.)
You can use re.groups() to get a tuple of all matches and then use relative addressing to get the last.
Example:
>>> s='123456789'
>>> m=re.search(r'(\d)(\d)(\d)', s)
>>> m.groups()
('1', '2', '3')
>>> m.groups()[-1]
'3'
Same as Perl:
% echo '123456' | perl -lnE 'say $ if /(\d)(\d)(\d)/'
3