5.7. Regexp Groups¶
5.7.1. About¶
(?P<name>...)
- Define named group(?P=name)
- Backreferencing by group name\number
- Backreferencing by group number
Syntax |
Description |
---|---|
|
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 |
Example:
(?P<tag><.*?>)text(?P=tag)
(?P<tag><.*?>)text\1
(.+) \1
matchesthe the
or55 55
(.+) \1
not matchesthethe
(note the space after the group)
5.7.2. Examples¶
Usage of group in re.match()
:
import re
PATTERN = r'(?P<firstname>\w+) (?P<lastname>\w+)'
TEXT = 'Jan Twardowski'
matches = re.match(PATTERN, TEXT)
matches.group('firstname') # 'Jan'
matches.group('lastname') # 'Twardowski'
matches.group(1) # 'Jan'
matches.group(2) # 'Twardowski'
matches.groups() # ('Jan', 'Twardowski')
matches.groupdict() # {'firstname': 'Jan', 'lastname': 'Twardowski'}