Cheat Sheet

Regex Cheat Sheet — Complete Reference (PCRE, JavaScript, Python)

Complete regular expression cheat sheet with every anchor, character class, quantifier, group, and flag — plus 30+ ready-to-use patterns for emails, URLs, dates, and more.

Everything you need to write and read regular expressions, on one page. Works for JavaScript, Python, PCRE, and most modern regex engines — dialect-specific notes are called out where they matter. Try any pattern immediately in the regex tester.

Character classes

TokenMatches
.Any character except newline (add s flag for dotall)
\wWord character: letters, digits, underscore
\WNon-word character
\dDigit (0-9)
\DNon-digit
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Anything except a, b, or c
[a-z]Any lowercase letter
[A-Z]Any uppercase letter
[0-9]Any digit (same as \d)
\p{L}Any Unicode letter (needs u flag)
\p{N}Any Unicode number
\p{Emoji}Any emoji (Unicode-aware engines)

Anchors & boundaries

TokenMatches
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary
\AStart of string only (not per-line, even with m)
\ZEnd of string only

Quantifiers

TokenMeaning
*0 or more (greedy)
+1 or more (greedy)
?0 or 1 (optional)
{n}Exactly n
{n,}n or more
{n,m}Between n and m
*? +? ?? {n,m}?Lazy (non-greedy) versions — match as little as possible
*+ ++Possessive (PCRE/Java only) — no backtracking

Groups & references

SyntaxMeaning
(abc)Capturing group
(?:abc)Non-capturing group (no backref slot)
(?<name>abc)Named capturing group
\1 \2Back-reference to group 1, 2, …
\k<name>Back-reference to named group
$1 $2Reference in replacement string
a|bAlternation (a OR b)

Lookarounds

SyntaxMeaningExample
(?=...)Lookahead: followed by\d+(?= dollars)
(?!...)Negative lookaheadfoo(?!bar)
(?<=...)Lookbehind: preceded by(?<=\$)\d+
(?<!...)Negative lookbehind(?<!no )good

Flags (modifiers)

FlagEffect
gGlobal — find all matches, not just first
iCase-insensitive
mMulti-line — ^/$ match line boundaries
sDotall — . matches newlines
uUnicode — enables \p{...}
ySticky — match from lastIndex only (JS)
xExtended — whitespace + comments inside pattern (PCRE, Python)

Ready-to-use patterns

PatternRegex
Email (practical)[^\s@]+@[^\s@]+\.[^\s@]+
URL (http/https)https?:\/\/\S+
IPv4^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
UUID^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$
Hex color^#(?:[0-9a-fA-F]{3}){1,2}$
Date YYYY-MM-DD^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Time HH:MM (24h)^([01]\d|2[0-3]):[0-5]\d$
US phone^\+?1?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
Password (8+ with mix)^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
Positive integer^[1-9]\d*$
Integer (with sign)^-?\d+$
Float^-?\d+(\.\d+)?$
Slug (kebab-case)^[a-z0-9]+(?:-[a-z0-9]+)*$
Leading/trailing whitespace^\s+|\s+$
Consecutive duplicates(.)\1+
HTML tag (simple)<\/?[a-zA-Z][^>]*>
Credit card (generic)^\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}$
Extract quoted string"([^"\\]*(?:\\.[^"\\]*)*)"

Common gotchas

  • Greedy by default. .* matches as much as possible; use .*? for the shortest match.
  • Escape specials: . * + ? ( ) [ ] { } | \ ^ $ / need a backslash when literal.
  • Inside character classes most specials lose their meaning; only - ] \ and ^ (at start) need escaping.
  • Don't parse HTML with regex — use a parser. Regex handles flat text, not nested markup.
  • Lookbehind limits — JavaScript supports variable-width lookbehind since 2018; older engines require fixed width.
  • Performance trap: nested quantifiers like (a+)+ cause catastrophic backtracking. Use possessive quantifiers or rewrite the pattern.

Test any pattern

Drop any pattern from this page into the regex tester — matches highlight live, capture groups extract separately, and flags toggle from a menu. For diffing two files line-by-line (a common regex alternative), use diff checker. For side-by-side text comparison, text compare.

Featured Tools

Try these free tools directly in your browser — no sign-up required.

regex cheat sheet regular expression cheat sheet regex reference regex syntax pcre cheat sheet javascript regex cheatsheet

Explore 300+ Free Tools

Utilko has tools for developers, writers, designers, students, and everyday users — all free, all browser-based.