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
Character Classes
Match sets and ranges of characters.
Quantifiers
Control how many times a token repeats.
Groups and Capturing
Capture, non-capture, and named groups.
Lookaround
Zero-width assertions around a position.
Anchors
Match positions instead of characters.
Backreferences
Reuse a previously captured group.