Regex Grouping and Capturing

Carson West

Regex Metacharacters

Regex Grouping and Capturing

This note covers grouping and capturing in Regular Expressions using Python’s re module.

Core Concepts:

Syntax:

import re

text = "My phone number is 123-456-7890, and my zip code is 90210."

# Grouping and capturing phone number
match = re.search(r"(\d{3})-(\d{3})-(\d{4})", text) 

if match:
    area_code, exchange, line_number = match.groups()
    print(f"Area Code: {area_code}, Exchange: {exchange}, Line Number: {line_number}")

#Capturing multiple matches with findall
matches = re.findall(r"(\d{5})", text) #finds all 5 digit numbers
print(f"Zip codes found: {matches}")


#Named capturing groups (Python 3.6+)
match = re.search(r"(?P<area_code>\d{3})-(?P<exchange>\d{3})-(?P<line_number>\d{4})", text)
if match:
    print(f"Area Code: {match.group('area_code')}, Exchange: {match.group('exchange')}, Line Number: {match.group('line_number')}")

Named Capture Groups: These are more readable and convenient for accessing captured groups by name instead of index. See example in the code above.

Applications:

Regex re.sub() function (Regex re.findall() function) Python’s re Module