Mastering Grouping and Capturing in Python Regular Expressions

Simplify Pattern Extraction and Manipulation Like a Pro

Max N
2 min readApr 6, 2024

Regular expressions in Python offer powerful capabilities for pattern matching and extraction. One of the most useful features is grouping and capturing, which allows you to extract specific parts of a matched pattern.

In this guide, we’ll explore how to effectively use grouping and capturing in Python regular expressions, with practical examples.

Understanding Grouping

Grouping in regular expressions is achieved by enclosing parts of the pattern within parentheses (). This allows you to treat multiple characters as a single unit.

import re

text = 'John has 3 cats and Mary has 2 dogs'

# Extracting names and corresponding counts using grouping
pattern = '(\w+) has (\d+) (\w+)'
matches = re.findall(pattern, text)
for match in matches:
print(f'{match[0]} has {match[1]} {match[2]}')

# Output:
# John has 3 cats
# Mary has 2 dogs

In this example, we use grouping to capture the name (\w+), count (\d+), and animal type (\w+), making it easier to extract and manipulate this information.

Capturing Matches

--

--

Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.