ToolzYard Blog

Developer guides and tutorials

Text & Regex Guide

Regex Cheat Sheet: Common Patterns Every Developer Should Know

Published: June 26, 2026 • By ToolzYard

Regular expressions are one of those skills that feel cryptic until they suddenly click — and then you wonder how you ever lived without them. A regex is a compact pattern for matching, searching, and replacing text, and the same core syntax works across JavaScript, Python, Java, PHP, Go, and almost every text editor and command-line tool. This cheat sheet covers the pieces you actually use day to day, with short examples you can test as you read.

Tip: keep the Regex Tester open in another tab and paste each pattern as you go. Seeing matches highlight live is the fastest way to build intuition.

Character classes

Character classes match "one character of a certain kind."

PatternMatches
.Any character except a newline
\dAny digit (0–9)
\DAny non-digit
\wWord character (letters, digits, underscore)
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-whitespace
[abc]Any one of a, b, or c
[^abc]Any character except a, b, or c
[a-z]Any lowercase letter in the range

Quantifiers

Quantifiers say "how many" of the preceding item to match.

PatternMeaning
*0 or more
+1 or more
?0 or 1 (optional)
{3}Exactly 3
{2,5}Between 2 and 5
{2,}2 or more

By default quantifiers are greedy — they match as much as possible. Add a ? to make them lazy (match as little as possible), which matters a lot when parsing tags or quotes:

Greedy:  <.+>   on  "<a><b>"  matches  "<a><b>"
Lazy:    <.+?>  on  "<a><b>"  matches  "<a>"  then  "<b>"

Anchors and boundaries

Anchors do not match characters; they match positions.

PatternMatches at
^Start of the string (or line in multiline mode)
$End of the string (or line)
\bA word boundary
\BNot a word boundary

For example, \bcat\b matches "cat" as a whole word but not the "cat" inside "category" or "scatter."

Groups and alternation

PatternMeaning
(abc)Capturing group — remembers the match
(?:abc)Non-capturing group — groups without remembering
(?<name>abc)Named capturing group
a|bMatch a or b (alternation)
\1Backreference to the first captured group

Groups are what make find-and-replace powerful. With capturing groups you can reorder a date from YYYY-MM-DD to DD/MM/YYYY in a single replace operation using $1, $2, and $3 in the replacement string.

Flags

FlagEffect
gGlobal — find all matches, not just the first
iCase-insensitive
mMultiline — ^ and $ match each line
sDot matches newlines too

Ready-to-use patterns

These are practical starting points. Adapt them to your needs and always test against real data — there is no single "perfect" pattern for messy real-world input.

Email (practical, not RFC-perfect)

^[\w.+-]+@[\w-]+\.[\w.-]+$

URL (http/https)

^https?:\/\/[^\s/$.?#].[^\s]*$

Date (YYYY-MM-DD)

^\d{4}-\d{2}-\d{2}$

Time (24-hour HH:MM)

^([01]\d|2[0-3]):[0-5]\d$

Hex color

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

Strong-ish password (8+ chars, upper, lower, digit)

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

That last one uses lookahead: (?=...) checks that a condition holds at the current position without consuming characters. Three lookaheads stacked together require a lowercase letter, an uppercase letter, and a digit, while .{8,} enforces the length.

Three mistakes to avoid

Test and build your patterns

The fastest way to learn regex is to experiment with instant feedback. These tools help:

Conclusion

Regex rewards a small amount of memorization with a huge amount of power. Learn the character classes, quantifiers, anchors, and groups in this sheet and you can already handle the vast majority of real tasks: validation, search-and-replace, log parsing, and data cleanup. Keep a tester handy, build patterns incrementally, and reach for a proper parser when your input is genuinely nested.

Frequently Asked Questions

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers match as much text as possible; lazy quantifiers (adding ?) match as little as possible. This matters when a pattern could match multiple lengths.

What does \b mean in regex?

\b matches a word boundary — the position between a word character and a non-word character. It lets you match whole words.

Why should I not parse HTML with regex?

HTML is nested and irregular, which regex cannot reliably handle. Use a dedicated HTML parser instead; regex is best for flat, predictable text.

Are regex patterns the same in every language?

The core syntax is largely shared, but some features and flags differ between engines (for example, JavaScript, PCRE, and Python). Most patterns in this sheet work everywhere.

How do I test a regex quickly?

Paste your pattern and sample text into an online Regex Tester to see matches highlighted in real time.