Toolzer

Developer Reference

Regex Cheatsheet — Complete Reference

Regex is the single most portable pattern-matching syntax in programming — the same expressions work in JavaScript, Python, grep, and every code editor. This cheatsheet covers everything you'll actually use, grouped by category.

Character classes

TokenMatches
.Any character except newline
\dDigit 0–9
\DNon-digit
\wWord char (letter, digit, _)
\WNon-word char
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any of a, b, c
[^abc]Anything except a, b, c
[a-z]Range a through z

Quantifiers

TokenMatches
*0 or more
+1 or more
?0 or 1
{n}Exactly n
{n,}n or more
{n,m}Between n and m
*?0 or more (lazy)
+?1 or more (lazy)

Anchors & boundaries

TokenMatches
^Start of string (or line in multiline)
$End of string (or line in multiline)
\bWord boundary
\BNon-word-boundary

Groups & lookaround

TokenMatches
(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named group
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind
\1Backreference to group 1

Common patterns

Email (rough)

^\S+@\S+\.\S+$

URL

https?:\/\/[^\s]+

IPv4

\b(?:\d{1,3}\.){3}\d{1,3}\b

Hex color

#(?:[0-9a-fA-F]{3}){1,2}\b

ISO date

\d{4}-\d{2}-\d{2}

Phone (E.164)

\+?[1-9]\d{1,14}

Slug

^[a-z0-9]+(?:-[a-z0-9]+)*$

Username (3–20 alnum/_/-)

^[A-Za-z0-9_-]{3,20}$

Common pitfalls

  • Greedy vs lazy: .* in HTML often grabs way more than you want. Use .*? or a negated character class.
  • Backtracking bombs: nested quantifiers like (a+)+ can hang on adversarial input.
  • Escape special chars: . + * ? ^ $ ( ) [ ] { } | \\ must be escaped in literal patterns.
  • Multiline flag: ^ and $ match line boundaries only with the m flag.

Try it on Toolzer

Frequently asked questions

Which regex flavor should I learn?+

PCRE (Perl-Compatible Regular Expressions) is the closest to a lingua franca — JavaScript, Python, PHP, Ruby, and grep all support most of PCRE. Learn PCRE first, then pick up your language's quirks.

Why is my regex slow?+

Usually catastrophic backtracking. Patterns like (a+)+ or nested quantifiers can take exponential time on non-matching input. Prefer atomic groups (?>...) and possessive quantifiers when available.

How do I match a URL?+

Don't try to handle every edge case with a regex — URL parsing has 40+ RFCs. Use a URL parser (URL constructor in JS, urllib in Python). Regex is fine for a rough extraction: /https?:\/\/\S+/.

What's the difference between .* and .*?+

The first is greedy — it matches as much as possible. The second (with ?) is lazy — it matches as little as possible. Use lazy quantifiers when scanning HTML/JSON to avoid grabbing too much.

Should I validate emails with regex?+

For UI hints, yes — a simple /^\S+@\S+\.\S+$/ catches typos. For real validation, send a confirmation email. The full RFC 5322 email regex is 6,000+ characters and still not comprehensive.