[Python] Getting Match Position and Content with Regex: List of Match Object Methods

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

  1. Main Methods of the Match Object
  2. Implementation Example: Extracting and Locating Product Codes
  3. Source Code
  4. Execution Result
  5. 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.

MethodReturn TypeDescription
group()strReturns the entire matched string (or a specified group).
groups()tupleIf the pattern contains groups (), returns a tuple containing all matched subgroups.
start()intReturns the starting index of the matched string.
end()intReturns the ending index of the matched string.
span()tupleReturns 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.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次