Comprehensive tutorial on Python's Match.lastgroup attribute with practical examples
last modified April 20, 2025
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.
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.
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.
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+\skg)|(?P<lbs>\d+\slbs)’)
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.
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.
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.
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.
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.
When using Match.lastgroup, consider these best practices:
Use meaningful names for groups to make lastgroup more useful
Check for None when patterns might match unnamed groups
Combine with groupdict for comprehensive match analysis
Prefer named groups when using alternations for better debugging
Document group names in complex patterns for maintainability
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.
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.
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.