5.7. Regexp Groups

5.7.1. About

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

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

  • \number - Backreferencing by group number

Table 5.11. Regular Expression Groups




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


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


A backreference to a named group


Matches the contents of the group of the same number


  • (?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)

5.7.2. Examples

Listing 5.70. 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'}