ZetCode

Python Match.lastgroup Attribute

last modified April 20, 2025

Introduction to Match.lastgroup

The Match.lastgroup attribute is part of Python's re module. It returns the name of the last matched capturing group in a regex.

This attribute is useful when working with named groups in complex patterns. It helps identify which specific group caused the match in alternations.

If no named groups matched or the pattern has no named groups, lastgroup returns None. It only works with successful matches.

Basic Syntax

The syntax for accessing Match.lastgroup is straightforward:

match.lastgroup

Here, match is a match object returned by functions like search or match. The attribute is read-only.

Basic Named Group Matching

Let's start with a simple example of using lastgroup with named groups.

basic_lastgroup.py
#!/usr/bin/python

import re

text = "Date: 2023-12-25"
pattern = re.compile(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})')

match = pattern.search(text)
if match:
    print(f"Last matched group name: {match.lastgroup}")
    print(f"Value: {match.group(match.lastgroup)}")

This example shows how to access the name of the last matched group. The pattern has three named groups for date components.

pattern = re.compile(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})')

We define a pattern with three named groups using the (?P<name>...) syntax. Each group captures part of a date.

print(f"Last matched group name: {match.lastgroup}")

This prints the name of the last group that participated in the match, which would be 'day' in this case.

Using lastgroup with Alternation

lastgroup is particularly useful with alternation patterns.

alternation.py
#!/usr/bin/python

import re

texts = ["10 kg", "20 lbs", "15 stones"]
pattern = re.compile(r'(?P<kg>\d+\s*kg)|(?P<lbs>\d+\s*lbs)')

for text in texts:
    match = pattern.search(text)
    if match:
        unit = match.lastgroup
        value = match.group(unit).split()[0]
        print(f"Found {value} in {unit}")

This example demonstrates how lastgroup identifies which unit of measurement was matched. The pattern has two alternatives.

When the input matches either 'kg' or 'lbs', lastgroup tells us which alternative succeeded. This makes pattern analysis easier.

Handling No Named Groups

When no named groups are present, lastgroup returns None.

no_named_groups.py
#!/usr/bin/python

import re

text = "The answer is 42"
pattern = re.compile(r'(\d+)')

match = pattern.search(text)
if match:
    print(f"Matched value: {match.group()}")
    print(f"Last group name: {match.lastgroup}")  # None

This shows that lastgroup returns None when the pattern contains only unnamed groups. The match still works normally.

Combining Named and Unnamed Groups

lastgroup only considers named groups, ignoring unnamed ones.

mixed_groups.py
#!/usr/bin/python

import re

text = "Color: #FF5733"
pattern = re.compile(r'Color:\s*((?P<hex>#[\da-fA-F]{6})|(?P<rgb>rgb\(\d+,\d+,\d+\)))')

match = pattern.search(text)
if match:
    print(f"Matched format: {match.lastgroup}")  # 'hex'
    print(f"Full match: {match.group()}")

Here we have a pattern with both named and unnamed groups. lastgroup correctly identifies the named group that matched.

Multiple Matches and lastgroup

When using finditer, each match has its own lastgroup.

multiple_matches.py
#!/usr/bin/python

import re

text = "10kg 20lbs 15kg 30lbs"
pattern = re.compile(r'(?P<kg>\d+kg)|(?P<lbs>\d+lbs)')

for match in pattern.finditer(text):
    print(f"Value: {match.group()}, Unit: {match.lastgroup}")

This example processes multiple matches in text. Each match object maintains its own lastgroup information.

The output shows which unit was matched for each value in the input string. This is useful for processing mixed-format data.

Complex Pattern with Nested Groups

lastgroup works correctly even with nested group structures.

nested_groups.py
#!/usr/bin/python

import re

text = "Product: Laptop (Model: XPS-15)"
pattern = re.compile(r'Product:\s*(?P<type>\w+)\s*(\(Model:\s*(?P<model>[\w-]+)\))?')

match = pattern.search(text)
if match:
    print(f"Last matched group: {match.lastgroup}")  # 'model'
    print(f"Product type: {match.group('type')}")
    print(f"Model: {match.group('model')}")

This demonstrates that lastgroup correctly identifies the deepest named group that participated in the match.

Best Practices

When using Match.lastgroup, consider these best practices:

Performance Considerations

Accessing lastgroup has minimal performance impact as the information is stored during the match process. It doesn't require additional pattern processing.

However, using many named groups in complex patterns may slightly increase memory usage. This tradeoff is usually worth the improved code clarity.

Source

Python Match.lastgroup documentation

This tutorial covered the essential aspects of Python's Match.lastgroup attribute. Mastering this feature will help you work more effectively with complex regular expressions.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Python tutorials.