Skip to content
Regex

Alternation

Match one of several branches.

By EZ4Code Team
alternationor

Code

cat|dog           cat or dog
(ab|cd)ef         abef or cdef
gr(a|e)y          gray or grey
^(From|Subject):  line starts with From: or Subject:
\b(cat|dog)\b     whole word cat or dog
(?|a(b)|c(d))     branch reset (PCRE)

Explanation

The pipe | separates alternatives and tries each branch left to right, stopping at the first match. Alternation has very low precedence, so grouping with parentheses is usually needed to scope it. The branch reset (?|...) in PCRE reuses group numbers across alternatives so capture indices stay stable.

More Regex Snippets