5. Groups & Alternation
Capturing, structuring, and reusing parts of a match
Parentheses (...) in regex do two things at once: they group elements together so
quantifiers and alternation apply to the whole unit, and they capture the matched text
so you can reference it later. This article covers both roles, plus alternation, named
groups, non-capturing groups, and backreferences.
Grouping for Quantifiers
Without parentheses, a quantifier applies to the single element before it. Parentheses let you apply a quantifier to an entire sequence.
Without grouping
abc+
Means: ab followed by one or more cs. Matches abc, abcc, abccc...
With grouping
(abc)+
Means: the sequence "abc" one or more times. Matches abc, abcabc, abcabcabc...
Pattern
(ha)+
Input
hahaha he laughed
Matches
hahaha he laughed
Alternation: |
The pipe | means "or." It matches the expression on the left or the expression on
the right.
Pattern
cat|dog
Input
I have a cat and a dog
Matches
I have a cat and a dog
Important: alternation has low precedence. The pattern abc|def means "abc OR def",
not "ab(c OR d)ef." Use parentheses to constrain the scope:
Pattern
gr(a|e)y
Matches
gray, grey
Why
The alternation is scoped to just the single character inside the parentheses.
For single-character alternation like this, a character class gr[ae]y is simpler and
equivalent.
Capturing Groups
Every pair of parentheses creates a capture group. The engine remembers what text the group matched, and you can reference it by number.
Groups are numbered left-to-right by their opening parenthesis:
Pattern
(\d{4})-(\d{2})-(\d{2})
Input
2026-04-24
Captures
- Group 0 (full match):
2026-04-24 - Group 1:
2026 - Group 2:
04 - Group 3:
24
In most languages, you access captures through the match object:
const match = "2026-04-24".match(/(\d{4})-(\d{2})-(\d{2})/);
// match[1] = "2026"
// match[2] = "04"
// match[3] = "24"
import re
m = re.match(r"(\d{4})-(\d{2})-(\d{2})", "2026-04-24")
# m.group(1) = "2026"
# m.group(2) = "04"
# m.group(3) = "24"
Captures in Replacement
Most regex-enabled find-and-replace tools let you reference captures in the replacement
string with $1, $2, etc. (or \1, \2 in some flavors):
Find
(\w+), (\w+)
Replace
$2 $1
Input
Doe, John
Result
John Doe
Named Groups
Instead of remembering that group 3 is the day, you can give groups names:
| Syntax | Flavor |
|---|---|
(?<name>...) |
JavaScript, .NET, Java, PCRE |
(?P<name>...) |
Python, PCRE |
Pattern
(?\d{4})-(?\d{2})-(?\d{2})
Input
2026-04-24
Access
const { year, month, day } = "2026-04-24".match(
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
).groups;
// year = "2026", month = "04", day = "24"
Named groups make complex patterns much more readable and maintainable.
Non-Capturing Groups: (?:...)
Sometimes you need parentheses for grouping or alternation but don't care about capturing the result. Non-capturing groups avoid the overhead:
Pattern
(?:https?|ftp)://\S+
What it does
Matches URLs starting with http, https, or ftp.
The (?:...) groups the protocol alternatives without creating a capture.
When to use non-capturing groups:
- When you only need grouping for structure or quantifiers
- When you have many groups and want to keep capture numbering clean
- In performance-sensitive patterns with many iterations
Backreferences
A backreference matches the same text that a previous capture group matched — not the same pattern, the same text.
Pattern
(\w+) \1
Input
the the quick brown fox fox
Matches
the the quick brown fox fox
Why
\1 refers back to whatever group 1 captured. If group 1 matched "the",
then \1 looks for the literal text "the" — so the pattern finds repeated
words.
Named backreferences use \k<name>:
Pattern
(?\w+) \k
Same behavior
Finds repeated adjacent words, using a named reference instead of \1.
Backreference Gotcha
Backreferences match the captured text, not the captured pattern. If group 1
captured "cat", then \1 matches only "cat" — not "dog" or any other \w+ match.
Pattern
<(\w+)>.*\1>
Input
<b>bold</b>
Matches
Yes — group 1 captures b, and \1 matches the b
in the closing tag.
Input
<b>bold</i>
Matches
No — group 1 captures b, but the closing tag has i.
Nested Groups
Groups can nest. Numbering follows the order of opening parentheses, left to right:
Pattern
((a)(b(c)))
Input
abc
Groups
- Group 1:
abc(outer group) - Group 2:
a - Group 3:
bc - Group 4:
c
Key Takeaways
(...)groups elements for quantifiers and captures the matched text.|is alternation (OR) — use parentheses to control its scope.- Captures are numbered by opening-parenthesis order, starting at 1.
- Named groups
(?<name>...)make patterns self-documenting. - Non-capturing groups
(?:...)group without capturing — use when you don't need the match. - Backreferences
\1or\k<name>match the exact text previously captured by a group.
Next up: lookaround — assertions that check what's ahead or behind without consuming characters.