Regex Cheat Sheet

A searchable reference of character classes, anchors, quantifiers, groups, flags, and common ready-to-use patterns.

Character classes

.Any character except newline
\dAny digit (0-9)
\DAny non-digit
\wWord character (letters, digits, underscore)
\WNon-word character
\sWhitespace character
\SNon-whitespace character
[abc]Any of a, b, or c
[^abc]Any character except a, b, or c
[a-z]Any lowercase letter

Anchors

^Start of string (or line, with m flag)
$End of string (or line, with m flag)
\bWord boundary
\BNot a word boundary

Quantifiers

*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?0 or more, lazy (shortest match)

Groups & alternation

(abc)Capture group
(?:abc)Non-capturing group
(?<name>abc)Named capture group
a|bMatch a or b
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind

Flags

gGlobal — find all matches, not just the first
iCase-insensitive
mMultiline — ^ and $ match line boundaries
sDot matches newline too
uUnicode mode

Common patterns

^[\w.-]+@[\w.-]+\.\w+$Basic email address
^https?:\/\/.+URL starting with http(s)
^\d{3}-\d{3}-\d{4}$US phone number (123-456-7890)
^#([0-9a-fA-F]{3}){1,2}$Hex color code
^\d{4}-\d{2}-\d{2}$ISO date (YYYY-MM-DD)

Example input

lookahead

Example output

(?=abc) — Positive lookahead