ID EN
Regular Expression

__getitem__

Python 3.11

This is identical to m.group(g). This allows easier access to an individual group from a match:

Syntax

PYTHON
Match.__getitem__(g)

Examples

Example 1
PYTHON
>>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
>>> m[0]        The entire match
'Isaac Newton'
>>> m[1]        The first parenthesized subgroup.
'Isaac'
>>> m[2]        The second parenthesized subgroup.
'Newton'
Example 2
PYTHON
>>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Isaac Newton")
>>> m['first_name']
'Isaac'
>>> m['last_name']
'Newton'