Regex Cheat Sheet: Every Pattern You Need with Examples

5 min read
Intermediate Regex Programming Reference

Regular expressions are one of those tools that feel like magic once you learn them and absolute nonsense before that. This cheat sheet is designed to be a practical reference — not a textbook. Every pattern includes a real example so you can see what it actually matches.

Bookmark this page. You will come back to it.

Basic Matchers

Pattern Matches Example
hello Literal text "hello" hello worldhello
. Any single character (except newline) h.t matches hat, hit, hot
\. A literal dot 3\.14 matches 3.14

The backslash \ escapes special characters. If you want to match an actual dot, question mark, or bracket, put \ before it.

Character Classes

Pattern Matches Example
[abc] Any one of a, b, or c [bc]at matches bat, cat
[^abc] Any character except a, b, c [^0-9] matches non-digits
[a-z] Any lowercase letter [a-z]+ matches hello
[A-Z] Any uppercase letter [A-Z][a-z]+ matches Hello
[0-9] Any digit [0-9]{3} matches 123
[a-zA-Z0-9] Any alphanumeric character Letters and digits

Shorthand Classes

Pattern Equivalent Matches
\d [0-9] Any digit
\D [^0-9] Any non-digit
\w [a-zA-Z0-9_] Word character
\W [^a-zA-Z0-9_] Non-word character
\s [ \t\n\r\f] Whitespace
\S [^ \t\n\r\f] Non-whitespace

Quantifiers

Pattern Meaning Example
* Zero or more ab*c matches ac, abc, abbc
+ One or more ab+c matches abc, abbc (not ac)
? Zero or one (optional) colou?r matches color, colour
{3} Exactly 3 \d{3} matches 123
{2,5} Between 2 and 5 \d{2,5} matches 12, 12345
{3,} 3 or more \w{3,} matches words with 3+ chars

Greedy vs Lazy

By default, quantifiers are greedy — they match as much as possible. Add ? to make them lazy (match as little as possible).

Pattern Behavior Input: bold
<.*> Greedy Matches bold (entire string)
<.*?> Lazy Matches (first tag only)

This matters when parsing HTML, JSON, or any text with delimiters. Almost always use lazy quantifiers when matching between delimiters.

Anchors

Anchors match a position, not a character.

Pattern Matches Example
^ Start of string (or line with m flag) ^Hello matches "Hello world"
$ End of string (or line with m flag) world$ matches "Hello world"
\b Word boundary \bcat\b matches "cat" but not "category"
\B Not a word boundary \Bcat\B matches the "cat" in "concatenate"

Groups and Capturing

Pattern Purpose Example
(abc) Capture group (ha)+ matches hahaha
(?:abc) Non-capturing group Groups without capturing
`(a\ b)` Alternation (OR) `(cat\ dog) matches cat or dog`
\1 Backreference to group 1 (\w+)\s+\1 matches the the

Named Groups

(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})

Matches 2026-03-15 and captures:

  • year = 2026
  • month = 03
  • day = 15

Lookahead and Lookbehind

These match a position based on what comes before or after, without including it in the match.

Pattern Type Meaning
(?=abc) Positive lookahead Followed by "abc"
(?!abc) Negative lookahead NOT followed by "abc"
(?<=abc) Positive lookbehind Preceded by "abc"
(? Negative lookbehind NOT preceded by "abc"

Examples

# Match "100" only if followed by "px"
100(?=px)          →  "100px" matches, "100em" does not

# Match a number NOT followed by "px"  
\d+(?!px)          →  Matches "200" in "200em"

# Match a price after "$"
(?<=\$)\d+\.\d{2}  →  Matches "49.99" in "$49.99"

Flags

Flag Name Effect
g Global Find all matches, not just the first
i Case insensitive a matches both a and A
m Multiline ^ and $ match start/end of each line
s Dotall . matches newlines too
u Unicode Enables full Unicode support

Common Patterns — Ready to Use

Email Address

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

Matches: [email protected], [email protected]

URL

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

Matches: https://www.samnet.dev/tools/, http://example.com

IPv4 Address

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

Matches: 192.168.1.1, 10.0.0.35

Phone Number (US)

\b\d{3}[-.]?\d{3}[-.]?\d{4}\b

Matches: 555-123-4567, 555.123.4567, 5551234567

Date (YYYY-MM-DD)

\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])

Matches: 2026-03-15, 2025-12-31

Hex Color Code

#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b

Matches: #ff6b6b, #00e0ff, #fff

HTML Tag

<\/?[\w\s="'-]+>

Matches:

,

,

Password Strength (min 8 chars, upper, lower, digit)

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

Matches strings with at least one lowercase, one uppercase, one digit, and 8+ characters.

Extract Domain from URL

https?:\/\/(?:www\.)?([^\/\s]+)

Group 1 captures: samnet.dev from https://www.samnet.dev/tools/

CSV Line Parser

(?:^|,)(?:"([^"]*(?:""[^"]*)*)"|([^,]*))

Handles quoted fields with escaped quotes inside CSV data.

Common Mistakes

Forgetting to escape special characters: . matches ANY character, not a literal dot. Use \. for dots, \? for question marks, \( for parentheses.

Greedy matching with HTML: <.> on bold matches the entire string, not just . Use <.?> instead.

Not anchoring: \d{3} matches "123" inside "12345". If you want exactly 3 digits, use ^\d{3}$ or \b\d{3}\b.

Overcomplicating email validation: The "perfect" email regex is thousands of characters long. The simple pattern above covers 99.9% of real email addresses. For true validation, send a confirmation email.

Test Your Patterns

Use our free Regex Tester to write and test regular expressions in real time. It highlights matches, shows capture groups, and supports all JavaScript regex flags. Paste any pattern from this cheat sheet and try it instantly.