When you execute re.search() or re.match() in Python’s re module, a Match object is returned if the match is successful.
This object holds detailed information not only about “whether it matched,” but also “where it matched” and “specifically what string was matched.”
In this article, I will explain the main methods for extracting information from the Match object, along with specific code examples.
Table of Contents
- Main Methods of the Match Object
- Implementation Example: Extracting and Locating Product Codes
- Source Code
- Execution Result
- Explanation
1. Main Methods of the Match Object
By using the following methods on the match result, you can retrieve the start position, end position, captured group content, and more.
| Method | Return Type | Description |
group() | str | Returns the entire matched string (or a specified group). |
groups() | tuple | If the pattern contains groups (), returns a tuple containing all matched subgroups. |
start() | int | Returns the starting index of the matched string. |
end() | int | Returns the ending index of the matched string. |
span() | tuple | Returns a tuple (start, end) of the matched string’s position. |
2. Implementation Example: Extracting and Locating Product Codes
Here is an example of searching for a product code with a specific format (e.g., 4 letters – 4 digits) within text data and retrieving its detailed information.
Source Code
import re
# Text to be analyzed
text = "New product released: PROD-2025 is available now."
# Regex pattern
# Defines two groups: (PROD) and (\d{4})
pattern = r"(PROD)-(\d{4})"
# Execute regex search
m_obj = re.search(pattern, text)
if m_obj:
# 1. Get the entire matched string
print(f"Match content : {m_obj.group()}")
# 2. Get match position information
print(f"Start index : {m_obj.start()}")
print(f"End index : {m_obj.end()}")
print(f"Span tuple : {m_obj.span()}")
# 3. Get strings for each group
# groups() returns all groups as a tuple
print(f"Groups : {m_obj.groups()}")
# Get individual groups using group(n) (1-based index)
print(f"Group 1 (Code): {m_obj.group(1)}")
print(f"Group 2 (Year): {m_obj.group(2)}")
else:
print("No match found.")
Execution Result
Match content : PROD-2025
Start index : 22
End index : 31
Span tuple : (22, 31)
Groups : ('PROD', '2025')
Group 1 (Code): PROD
Group 2 (Year): 2025
3. Explanation
Getting Position Information
By using start() and end(), you can pinpoint exactly where the pattern exists in the original string. This is useful when performing custom highlighting or string replacement.
span() returns a tuple equivalent to (start(), end()), which is convenient for slicing operations (e.g., text[s:e]).
Getting Groups
group(): If you omit the argument (or specify 0), it returns the entire match.groups(): Extracts only the defined subgroups (parts enclosed in parentheses), making it suitable for parsing structured data.
