0Pricing
Python Academy · Lesson

Groups and Named Captures

Extract data with capturing groups and named group syntax.

Introduction

Capture groups extract specific parts of a match. Named groups make the extracted data self-documenting.

Basic Capturing Group

Parentheses () create a capturing group. m.group(1) returns the first group's match.
import re
m = re.search(r'(\d{4})-(\d{2})-(\d{2})', '2024-01-15')
if m: print(m.group(1), m.group(2), m.group(3))

All lessons in this course

  1. Regex Fundamentals
  2. Using re.match, re.search, re.findall
  3. Groups and Named Captures
  4. re.sub, Flags, and Compiled Patterns
← Back to Python Academy