Why alternation matters
Alternation allows a regular expression to match one pattern or another. It introduces OR logic, making regexes flexible enough to support multiple valid formats.
Without alternation, many practical regexes would require duplicated patterns or multiple independent expressions.
Combined with groups, alternation keeps patterns concise, readable and easy to extend.
Group alternatives whenever possible
Alternation is often clearer when combined with groups.
Grouping ensures that the OR operator applies only to the intended part of the pattern.
Well-placed groups make complex expressions easier to read and less prone to subtle bugs.
Reduce duplication in alternatives
When several alternatives share a common prefix or suffix, factor the common part outside the alternation.
This reduces duplication, shortens the regex and makes future modifications much easier.
A concise regex is often easier to debug than a long list of repeated alternatives.
Order alternatives carefully
Some regex engines evaluate alternatives from left to right.
Placing the most specific or most common alternatives first can improve readability and sometimes performance.
Poor ordering may also produce unexpected matches when one alternative is a prefix of another.
Common alternation mistakes
A common mistake is forgetting parentheses, causing the alternation to apply to a larger part of the pattern than intended.
Another frequent issue is duplicating alternatives or creating overlapping branches.
Testing every branch independently helps reveal these mistakes quickly.
Alternation appears in many real-world regexes
Alternation is used in email validation, URLs, phone numbers, dates, identifiers and many other practical patterns.
Whenever several valid formats must be accepted, alternation is usually one of the simplest solutions.
Mastering alternation makes complex regexes easier to design and maintain.
Basic alternation with |
The | operator means OR.
The pattern cat|dog matches either cat or dog.
cat|dog
Alternation inside a group
Alternatives are often placed inside a group.
This keeps the alternatives limited to one part of the pattern.
gr(?:ay|ey)
Shared prefix
When alternatives share the same prefix, factor the common text outside the group.
This usually makes the pattern shorter and easier to maintain.
https?://(?:www\.)?(?:example|test)\.com
Shared suffix
Alternatives can also share the same suffix.
Grouping only the changing part avoids unnecessary duplication.
(?:cat|dog|bird)s
Alternation precedence
Alternation has low precedence.
Without grouping, abc|def matches either abc or def, not a shared surrounding pattern.
^(cat|dog)$
Multiple alternatives
You can define more than two alternatives.
Keep the list readable and consider sorting or grouping related options.
\b(?:red|green|blue|yellow)\b
Alternation with optional text
Alternation can represent several optional formats.
This is useful when an input accepts multiple separators or prefixes.
^\d{4}(?:-|/|\.)\d{2}(?:-|/|\.)\d{2}$
Capturing alternatives
A capturing group can store which alternative matched.
This can be useful when your code needs to react differently depending on the matched branch.
\b(yes|no|maybe)\b