Sign in Start free

10Capturing groups

Parentheses group tokens and remember what they matched as group 1, 2, …

Parentheses do two jobs: they group tokens (so a quantifier applies to all of them: (ha)+) and they capture the matched text. Groups are numbered by their opening parenthesis from left to right.

In (\d{4})-(\d{2})-(\d{2}) applied to 2026-11-12, group 1 is 2026, group 2 11 and group 3 12. You use captured groups in code, in replacements ($1) and in back references.

In the tester, the Match information panel lists every group of every match, and groups get their own colors in the test string.

Exercises