How regular expression grouping works in Python?

Regular expression grouping in Python allows you to capture specific parts of a pattern match using parentheses (). This feature is essential for extracting structured data from strings and organizing match results into meaningful components.

Understanding Regex Grouping Basics

Groups are created by placing parentheses around parts of your regex pattern. The re module provides two main methods for working with groups ?

  • group() − Returns the whole match or a specific group by number
  • groups() − Returns all captured groups as a tuple

Syntax

import re
match.group([group_number])
match.groups()

Group numbers start from 1, while group(0) returns the entire match.

Example 1: Extracting Name Components

Here's how to extract first and last names from a formatted string ?

import re

text = "Name: Ankit Sharma"
match = re.search(r"Name: (\w+) (\w+)", text)

# Whole match
print("Full match:", match.group(0))

# First name (group 1)
print("First name:", match.group(1))  

# Last name (group 2)
print("Last name:", match.group(2))
Full match: Name: Ankit Sharma
First name: Ankit
Last name: Sharma

Example 2: Email Address Parsing

Extract username, domain, and extension from an email address ?

import re

email = "Email: user@example.com"
match = re.search(r"Email: (\w+)@(\w+)\.(\w+)", email)

# All groups as tuple
print("All parts:", match.groups())

# Individual parts
print(f"Username: {match.group(1)}")
print(f"Domain: {match.group(2)}")
print(f"Extension: {match.group(3)}")
All parts: ('user', 'example', 'com')
Username: user
Domain: example
Extension: com

Example 3: Product Information Extraction

Parse product details from a structured string ?

import re

text = "Product: Mobile, Price: $500"
match = re.search(r"Product: (\w+), Price: \$(\d+)", text)

if match:
    product = match.group(1)
    price = match.group(2)
    print(f"Item: {product}, Price: ${price}")
Item: Mobile, Price: $500

Working with findall() and Groups

When using findall() with groups, it returns tuples of the captured groups ?

import re

text = "Temperature: 25.5°C, Humidity: 60.2%, Pressure: 1013.25 hPa"
matches = re.findall(r"(\w+): (\d+\.\d+)", text)

for measurement, value in matches:
    print(f"{measurement}: {value}")
Temperature: 25.5
Humidity: 60.2
Pressure: 1013.25

Key Points

  • Groups are numbered starting from 1
  • group(0) returns the entire match
  • groups() returns all groups as a tuple
  • Groups enable structured data extraction from text
  • Always check if match exists before accessing groups

Conclusion

Regular expression grouping is a powerful feature for extracting structured data from strings. Use group() to access specific parts and groups() to get all captured groups as a tuple.

Updated on: 2026-03-24T18:57:35+05:30

960 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements