What are regex capture groups?
Capture groups allow you to extract specific parts of a regex match for later use.
Basic capture groups:
Parentheses create capture groups:
(\d{3})-(\d{4})matches phone numbers and captures area code and number separately- First group captures
\d{3}, second group captures\d{4}
Accessing captured values:
After matching, you can access each group:
- In JavaScript:
match[1],match[2], etc. - In Python:
match.group(1),match.group(2), etc. - Group 0 is always the full match
Named capture groups:
Modern regex supports named groups for better readability:
(?<area>\d{3})-(?<number>\d{4})creates named groups- Access by name:
match.groups.areain JavaScript
Non-capturing groups:
Sometimes you need grouping without capturing:
(?:abc)+groups "abc" for repetition but doesn't create a capture- Useful for performance when you don't need the captured value
Common use cases:
- Extracting data from structured text (prices, dates, IDs)
- Parsing HTML or XML without a full parser
- Reformatting text by swapping captured groups
- Validating and extracting parts of user input
Capture groups are essential for web scraping when you need to extract multiple values from a single pattern match.