YouTip LogoYouTip

Regex Groups and Capturing

Introduction

Groups allow you to treat multiple characters as a single unit and capture matched text. They are essential for complex pattern matching and text extraction.

Capturing Groups

# Basic group
/(ab)+/           matches "ab", "abab", "ababab"

# Capture groups for extraction
/(d{3})-(d{4})/
# "555-1234" β†’ Group 1: "555", Group 2: "1234"

# In JavaScript
const match = "2024-06-12".match(/(d{4})-(d{2})-(d{2})/);
// match = "2024", match = "06", match = "12"

Named Groups

# Named capturing groups
/(?<year>d{4})-(?<month>d{2})-(?<day>d{2})/

# JavaScript
const match = "2024-06-12".match(
  /(?<year>d{4})-(?<month>d{2})-(?<day>d{2})/
);
// match.groups.year = "2024"

# Python
import re
m = re.search(r'(?P<year>d{4})-(?P<month>d{2})', "2024-06")
# m.group('year') = "2024"

Non-Capturing Groups

# (?:...) groups without capturing
/(?:https?://)?([w.]+)/
# Groups the protocol but doesn't capture it

# Useful for alternation without capture
/(?:jpg|png|gif)/

Backreferences

# Referencing captured groups
/(w+)s1/       matches repeated words
# "the the" matches, "the that" doesn't

# Find duplicate words
/b(w+)s+1b/g

Summary

Capturing groups extract matched text. Use named groups for readability, non-capturing groups for efficiency, and backreferences for pattern repetition.

← Regex Practical ExamplesRegex Quantifiers and Anchors β†’