Text

Regex Cheat Sheet for Developers — Patterns, Examples & Common Mistakes

📅 June 16, 2026⏱ 8 min read 🛠️ Try the Regex Tester →

Regular expressions are one of those things you learn just enough of, use once, and then forget immediately. This is the reference you'll actually bookmark — every metacharacter, the patterns you'll copy most often, and the mistakes that waste hours of debugging.

Metacharacters

PatternMatchesExample
.Any character except newlinea.c → abc, a1c, a-c
\dAny digit (0-9)\d{3} → 123, 456
\DAny non-digit\D+ → abc, ---
\wWord character (letter, digit, underscore)\w+ → hello, var_1
\WNon-word character\W → @, #, space
\sWhitespace (space, tab, newline)\s+ → " ", "\t"
\SNon-whitespace\S+ → hello
\bWord boundary\bcat\b → "cat" but not "scatter"

Quantifiers

PatternMeaningExample
*0 or more (greedy)a* → "", a, aaa
+1 or more (greedy)a+ → a, aaa (not "")
?0 or 1 (optional)colou?r → color, colour
{n}Exactly n times\d{4} → 2026
{n,}n or more times\d{2,} → 12, 123, 1234
{n,m}Between n and m times\d{2,4} → 12, 123, 1234
*?0 or more (lazy)Matches as few as possible
+?1 or more (lazy)Matches as few as possible

Anchors and Boundaries

PatternMeaning
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary — between \w and \W
\BNot a word boundary

Key distinction: ^abc matches "abc" only at the start of a string. abc without the anchor matches "abc" anywhere in the string.

Character Classes

PatternMatches
[abc]Any one of a, b, or c
[a-z]Any lowercase letter
[A-Z]Any uppercase letter
[0-9]Any digit (same as \d)
[a-zA-Z0-9]Any alphanumeric character
[^abc]Any character EXCEPT a, b, or c

Inside [], most special characters lose their meaning. A literal hyphen goes first or last: [-a-z] or [a-z-]. A literal ] goes first: []a-z].

Groups and Alternation

PatternMeaning
(abc)Capturing group — captures "abc"
(?:abc)Non-capturing group — groups without capturing
a|bAlternation — matches a OR b
\1Backreference — matches same text as group 1
(?<name>abc)Named group — captured as "name"
# Extract date parts with named groups
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
Input:  2026-06-15
Groups: year=2026, month=06, day=15

Greedy vs Lazy Matching

This is the #1 source of unexpected regex behavior:

Input: <b>hello</b> world <b>foo</b>

Greedy:  <b>.*</b>   → "<b>hello</b> world <b>foo</b>"  (entire string!)
Lazy:    <b>.*?</b>  → "<b>hello</b>"  (first match only)

Greedy (.*) matches as much as possible. Lazy (.*?) matches as little as possible. When you want the shortest match, always use the lazy version.

Lookahead and Lookbehind

PatternMeaningExample
(?=abc)Positive lookahead — followed by "abc"\d+(?= USD) → "100" in "100 USD"
(?!abc)Negative lookahead — NOT followed by "abc"\d+(?! USD) → "200" in "200 EUR"
(?<=abc)Positive lookbehind — preceded by "abc"(?<=\$)\d+ → "50" in "$50"
(?<!abc)Negative lookbehind — NOT preceded by "abc"(?<!\$)\d+ → "50" in "€50"

Lookarounds assert a condition without consuming characters — the matched text does not include the lookaround part.

Patterns You'll Copy the Most

WhatPattern
Date (YYYY-MM-DD)\d{4}-\d{2}-\d{2}
Email (basic)[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URL (http/https)https?://[^\s]+
IPv4 address\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Phone (US)\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Hex color#[0-9a-fA-F]{3,8}
HTML tag<[^>]+>
Blank lines^\s*$ (with m flag)
Trailing whitespace\s+$ (with m flag)
Quoted string"[^"]*" or '[^']*'

Important: the email regex above is a practical approximation, not RFC 5322 compliant. For production email validation, use your language's built-in validator or a library — regex alone cannot fully validate email addresses.

Regex Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitive/abc/i matches ABC, Abc, etc.
mMultiline^ and $ match line boundaries, not just string boundaries
sDotall. matches newline characters too

Common Mistakes

Frequently Asked Questions

Which regex flavor should I learn?
PCRE (Perl Compatible Regular Expressions) is the most widely used flavor. JavaScript, Python, Java and Go all use PCRE-like syntax with minor differences. Learn PCRE and you're covered for 95% of use cases.

How do I debug a complex regex?
Break it into parts and test each group separately. Use our Regex Tester to see matches highlighted in real time. For complex patterns, add comments using the x (verbose) flag.

Is there a performance cost to capturing groups?
Yes, capturing groups allocate memory to store the matched text. Use non-capturing groups (?:...) when you don't need the captured value. In performance-critical code, this matters.

Can regex match balanced brackets or nested structures?
Standard regex cannot. Some flavors (PCRE, .NET) support recursive patterns, but it's better to use a parser for nested structures like HTML, JSON or programming languages.

Test your regex patterns live — free

Paste a regex and test text to see matches highlighted in real time. Supports flags, groups, and substitution. Runs 100% in your browser.

Regex Tester → Regex Generator →