Skip to content
← Blog

RegEx Cheat Sheet for Developers

8 min readReference

Regular expressions (regex) are one of the most powerful text processing tools available to developers — and one of the most frequently forgotten. This cheat sheet covers the syntax you need for everyday pattern matching, from basic metacharacters to advanced lookahead assertions.

Basic Metacharacters

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

Quantifiers

PatternMeaning
*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n
{n,}n or more
{n,m}Between n and m
*?0 or more (lazy/non-greedy)
+?1 or more (lazy/non-greedy)

Greedy vs lazy: By default, quantifiers are greedy — they match as much as possible. Adding ? makes them lazy — matching as little as possible.

"<b>bold</b>" with <.*>  → matches "<b>bold</b>" (greedy)
"<b>bold</b>" with <.*?> → matches "<b>" (lazy)

Anchors

PatternMatches
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary

Character Classes

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

Groups and Alternation

PatternPurpose
(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
a|bAlternation (a or b)
\1Backreference to group 1

Lookahead and Lookbehind

PatternNameMatches
(?=abc)Positive lookaheadFollowed by "abc"
(?!abc)Negative lookaheadNOT followed by "abc"
(?<=abc)Positive lookbehindPreceded by "abc"
(?<!abc)Negative lookbehindNOT preceded by "abc"

Lookarounds are zero-width assertions — they check a condition without consuming characters.

\d+(?= dollars)     → "100" in "100 dollars"
(?<=\$)\d+           → "50" in "$50"
\b\w+(?!ing\b)       → words not ending in "ing"

Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitivea matches A
mMultiline^ and $ match line boundaries
sDotall. matches newline characters
uUnicodeFull Unicode support

Practical Patterns

Email (simplified)

[\w.-]+@[\w.-]+\.\w{2,}

URL

https?:\/\/[\w.-]+(?:\/[\w./?#&=-]*)?

IPv4 Address

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

Date (YYYY-MM-DD)

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

Phone Number (US)

(?:\+1[-.\s]?)?(?:\(?\d{3}\)?[-.\s]?)?\d{3}[-.\s]?\d{4}

Hex Color

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

HTML Tag

<\/?[\w]+(?:\s+[\w-]+(?:="[^"]*")?)*\s*\/?>

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

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

Common Mistakes

  1. Not escaping special characters., *, +, ?, (, ), [, {, \, ^, $, | all have special meaning. Escape them with \ when matching literally.
  2. Greedy matching.* in the middle of a pattern often matches more than expected. Use .*? for lazy matching or be more specific.
  3. Not anchoring — without ^ and $, a pattern can match anywhere in the string, not just the full string.
  4. Catastrophic backtracking — nested quantifiers like (a+)+b can cause exponential backtracking. Avoid patterns where the regex engine has too many ways to match.

Try It

Test your regex patterns with live highlighting using StackCache Regex Tester. See matches, capture groups, and named groups in real time — all locally in your browser.

Try it yourself

Open the tool mentioned in this guide — it runs locally in your browser, no account needed.

Open tool