Skip to content
TPToolPocket
← Back to Blog

Regex Cheat Sheet: The Most Useful Patterns

·7 min read

Regular expressions are one of the most powerful tools in a developer's toolkit — and one of the most confusing. This cheat sheet covers the patterns you'll actually use day to day.

Basic Matching

PatternMatchesExample
.Any character except newlineh.t matches "hat", "hot"
\dAny digit (0-9)\d\d matches "42"
\wWord character (letter, digit, _)\w+ matches "hello_42"
\sWhitespace (space, tab, newline)hello\sworld
\bWord boundary\bcat\b matches "cat" but not "catch"

Quantifiers

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

Character Classes

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

Anchors & Groups

PatternMeaning
^Start of string (or line with m flag)
$End of string (or line with m flag)
(abc)Capture group
(?:abc)Non-capturing group
a|bAlternation (a or b)

Real-World Patterns

Here are patterns you'll actually copy-paste:

PurposePattern
Email (simple)[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URLhttps?://[^\s]+
IP Address\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
Hex Color#[0-9a-fA-F]{3,8}
Phone (US)\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Date (YYYY-MM-DD)\d{4}-\d{2}-\d{2}

Flags

  • gGlobal: find all matches, not just the first
  • iCase-insensitive: A matches a
  • mMultiline: ^ and $ match line boundaries
  • sDotall: . matches newlines too

Test Your Regex

Try these patterns in our Regex Tester — it shows live matches, capture groups, and highlighted results as you type.