Numbered slots, left to right
Every capturing group gets a number based on the position of its OPENING parenthesis, counted left to right. Group 0 is always the entire match.
Pattern (\d{4})-(\d{2})-(\d{2}) against 2026-05-04:
- Group 0:
2026-05-04(whole match) - Group 1:
2026 - Group 2:
05 - Group 3:
04
Accessing groups
Most languages give you direct access to numbered groups:
- Python:
m.group(1),m.groups()for tuple of all - JavaScript:
match[1],match[2]... - Go:
m[1],m[2]... - ripgrep:
$1,$2in --replace mode
The findall surprise
Python's re.findall behaves quirkily with groups: when there are NO groups, it returns full matches; when there's ONE group, it returns just that group; when there are MULTIPLE groups, it returns tuples. Don't try to be clever — use re.finditer for predictable group access.