Visual regex workbench

Build, test, explain and master regex with visual tools, guides, examples and a growing regex library.

Regex guide

Regex Alternation

Alternation adds OR logic to a regular expression. It lets a pattern match one possibility or another, making it essential for formats, keywords, prefixes, suffixes and multiple accepted variants.

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.

Continue learning

πŸ“˜ Recommended guide

Groups

Alternation is usually combined with groups so the OR logic applies only to the intended part of the pattern.

Read the complete guide β†’
πŸ“˜ Recommended guide

Quantifiers

Quantifiers can apply to grouped alternatives, allowing an entire set of options to repeat or become optional.

Read the complete guide β†’
πŸ“˜ Recommended guide

Testing Regex

Alternation patterns should be tested with every accepted branch, invalid near-matches and values that share common prefixes.

Read the complete guide β†’
πŸ“˜ Recommended guide

Common Regex Mistakes

Many alternation bugs come from missing groups, duplicated branches or alternatives ordered in an unexpected way.

Read the complete guide β†’

Basic alternation with |

The | operator means OR.

The pattern cat|dog matches either cat or dog.

cat|dog
Quick test

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)
Quick test

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
Quick test

Shared suffix

Alternatives can also share the same suffix.

Grouping only the changing part avoids unnecessary duplication.

(?:cat|dog|bird)s
Quick test

Alternation precedence

Alternation has low precedence.

Without grouping, abc|def matches either abc or def, not a shared surrounding pattern.

^(cat|dog)$
Quick test

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
Quick test

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}$
Quick test

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
Quick test