Cheatsheet Pages / Regex Cheatsheet

How to match this or that in regex — | alternation

Two (or more) alternatives are equally acceptable and either one should match.

Patterns use the syntax shared by PCRE / Python / JavaScript; engine differences are flagged in the cheatsheet where they matter. Test any pattern at regex101.com — and pick the matching engine.

cat|dog
# "cat" or "dog"
gr(a|e)y
# "gray" or "grey" — the | only applies inside the group

Good to know

| has the lowest precedence of all — wrap it in (...) to limit its scope, as gr(a|e)y does.

Regex Cheatsheet one-page card — patterns that actually work
The full one-page card — Regex Cheatsheet. Get the complete cheatsheet on GitHub