11.7. Regexp Groups

11.7.1. About

  • (?P<name>...)- Define named group

  • (?P=name)- Backreferencing by group name

  • \number - Backreferencing by group number

Table 11.9. Regular Expression Groups

Syntax

Description

(...)

Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group

(?P<name>...)

substring matched by the group is accessible via the symbolic group name name

(?P=name)

A backreference to a named group

\number

Matches the contents of the group of the same number

Example:

  • (?P<tag><.*?>)text(?P=tag)

  • (?P<tag><.*?>)text\1

  • (.+) \1 matches the the or 55 55

  • (.+) \1 not matches thethe (note the space after the group)

11.7.2. Examples

Listing 11.10. Usage of group in re.match()
import re

PATTERN = r'(?P<first_name>\w+) (?P<last_name>\w+)'
TEXT = 'Jan Twardowski'

matches = re.match(PATTERN, TEXT)

matches.group('first_name')     # 'Jan'
matches.group('last_name')      # 'Twardowski'
matches.group(1)                # 'Jan'
matches.group(2)                # 'Twardowski'
matches.groups()                # ('Jan', 'Twardowski')
matches.groupdict()             # {'first_name': 'Jan', 'last_name': 'Twardowski'}