← helloandy.net

Regex Cheat Sheet

Complete regular expression reference — character classes, quantifiers, assertions, groups, and ready-to-use patterns. Bookmark this page.

Try the AI Regex Generator →

Character Classes

PatternDescriptionExample
.Any character except newlineh.t matches "hat", "hot", "h9t"
\dAny digit (0-9)\d{3} matches "123", "456"
\DAny non-digit\D+ matches "abc", "!@#"
\wWord character (a-z, A-Z, 0-9, _)\w+ matches "hello_42"
\WNon-word character\W matches "!", " ", "@"
\sWhitespace (space, tab, newline)\s+ matches " \t\n"
\SNon-whitespace\S+ matches "hello"
[abc]Any one of a, b, or c[aeiou] matches any vowel
[^abc]Not a, b, or c[^0-9] matches non-digits
[a-z]Any character in range[A-Za-z] matches any letter

Quantifiers

PatternDescriptionExample
*Zero or moreab*c matches "ac", "abc", "abbc"
+One or moreab+c matches "abc", "abbc" (not "ac")
?Zero or one (optional)colou?r matches "color", "colour"
{n}Exactly n times\d{4} matches "2026"
{n,}n or more times\w{3,} matches words ≥3 chars
{n,m}Between n and m times\d{2,4} matches "12", "123", "1234"
*?Zero or more (lazy)<.*?> matches shortest tag
+?One or more (lazy)".+?" matches shortest quoted string
Greedy vs. Lazy: By default, * and + are greedy — they match as much as possible. Add ? after them to make them lazy (match as little as possible). This matters when parsing HTML, JSON, or any nested structure.

Anchors & Boundaries

PatternDescriptionExample
^Start of string (or line with m flag)^Hello matches "Hello world"
$End of string (or line with m flag)world$ matches "Hello world"
\bWord boundary\bcat\b matches "cat" (not "category")
\BNot a word boundary\Bcat matches "concatenate"

Groups & References

PatternDescriptionExample
(abc)Capturing group(\d{3})-(\d{4}) captures area code + number
(?:abc)Non-capturing group(?:http|https):// groups without capturing
(?<name>abc)Named capture group(?<year>\d{4}) captures as "year"
\1Backreference to group 1(\w+)\s\1 matches "the the"
a|bAlternation (a or b)cat|dog matches "cat" or "dog"

Lookahead & Lookbehind

PatternDescriptionExample
(?=abc)Positive lookahead\d+(?= dollars) matches "100" in "100 dollars"
(?!abc)Negative lookahead\d+(?! dollars) matches "100" in "100 euros"
(?<=abc)Positive lookbehind(?<=\$)\d+ matches "50" in "$50"
(?<!abc)Negative lookbehind(?<!\$)\d+ matches "50" in "€50"
Lookarounds are zero-width assertions — they check a condition without consuming characters. Use them when you need to match something near another pattern without including that pattern in the result.

Flags

FlagNameEffect
gGlobalMatch all occurrences, not just the first
iCase-insensitive/hello/i matches "Hello", "HELLO"
mMultiline^ and $ match start/end of each line
sDotall. matches newline characters too
uUnicodeEnable full Unicode matching (emoji, CJK)

Special Characters (Escaping)

These characters have special meaning in regex. To match them literally, escape with backslash \:

. * + ? ^ $ { } [ ] ( ) | \ /

Example: to match a literal period, use \. instead of .

Common Patterns

Copy-paste ready patterns for everyday use. Test them in our Regex Generator.

Email Address
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Matches: user@example.com, name+tag@mail.co.uk
URL
https?://[^\s/$.?#].[^\s]*
Matches: https://example.com/path
IPv4 Address
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Matches: 192.168.1.1, 10.0.0.1
Phone Number (US)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches: (555) 123-4567, 555.123.4567
Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
Matches: 2026-03-14, 2025-12-31
Hex Color
#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b
Matches: #fff, #8b5cf6
HTML Tag
<([a-z]+)([^<]*)(?:>(.*?)<\/\1>|\/>)
Matches: <div class="x">...</div>
Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%]).{8,}$
Requires: lowercase, uppercase, digit, special char, 8+ length
Username
^[a-zA-Z0-9_-]{3,20}$
Matches: user_42, john-doe
Credit Card (Basic)
\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b
Matches: 4111 1111 1111 1111

Quick Tips

1. Start Simple, Add Complexity

Begin with a loose pattern that matches too much, then tighten it. It's easier to debug a pattern that matches too many things than one that matches nothing.

2. Use Non-Capturing Groups for Performance

If you don't need the captured text, use (?:...) instead of (...). This saves memory when processing large files.

3. Anchor When Possible

Use ^ and $ to anchor your pattern. ^\d{3}$ is much faster than \d{3} because the engine knows exactly where to look.

4. Be Careful with Backtracking

Nested quantifiers like (a+)+ can cause catastrophic backtracking (exponential time). Avoid patterns where a quantified group contains another quantifier matching the same characters.

5. Test with Edge Cases

Always test with empty strings, strings with only whitespace, very long inputs, and Unicode characters. Our Regex Generator makes this easy with live match highlighting.

Need help building a regex?

Describe what you want to match in plain English and our AI generates the pattern for you.

Open Regex Generator →

More Free Developer Tools

Regex Generator Cron Generator API Tester AI Chat CLAUDE.md Writer