Need to retrieve text from a regular-expression match? Call group() on the Match object. With no argument, it returns the complete match; with a number, it returns that capturing group. section 2.4.4
Retrieve the complete match and individual groups
A pattern containing two capturing groups lets you retrieve the entire match with group(0), or each captured value with group(1) and group(2): match = re.match(r"(\w+), (\w+)", "Homework, urgent; today") match.group(0) # 'Homework, urgent' match.group(1) # 'Homework' match.group(2) # 'urgent'
Use numbered groups when the pattern is small and stable. For structured records, group(1), group(2), and group(3) can extract fields such as an ID, title, and description, but counting positions becomes error-prone as patterns grow. section 2.5.2
Prefer named groups for maintainable patterns: define names with (?P<name>pattern), then pass those names to group(). When dictionary-style processing fits better, groupdict() returns the named captures. section 2.5.3

