ID EN
Regular Expression

group

Python 3.11

Returns one or more subgroups of the match. If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, group1 defaults to zero (the whole match is returned). If a groupN argument is zero, the corresponding return value is the entire matching string; if it is in the inclusive range [1..99], it is the string matching the corresponding parenthesized group. If a group number is negative or larger than the number of groups defined in the pattern, an IndexError exception is raised. If a group is

Syntax

PYTHON
Match.group([group1, ...])

Examples

Example 1
PYTHON
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m.group(0)        The entire match
'Isaac Newton'
>>> m.group(1)        The first parenthesized subgroup.
'Isaac'
>>> m.group(2)        The second parenthesized subgroup.
'Newton'
>>> m.group(1, 2)     Multiple arguments give us a tuple.
('Isaac', 'Newton')
Example 2
PYTHON
>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
>>> m.group('first_name')
'Malcolm'
>>> m.group('last_name')
'Reynolds'
Example 3
PYTHON
>>> m.group(1)
'Malcolm'
>>> m.group(2)
'Reynolds'
Example 4
PYTHON
>>> m = re.match(r"(..)+", "a1b2c3")   Matches 3 times.
>>> m.group(1)                         Returns only the last match.
'c3'

See Also

IndexError IndexError