·7 min read·Blog

Regex Cheat Sheet for Beginners: The Patterns You'll Actually Use

90% of regex use cases are covered by the same 20 patterns. Here's the practical subset — not an exhaustive reference — with real examples and a live tester to try them immediately.

Test every pattern in this guide using the free regex tester — paste the pattern, enter test strings, and see matches highlighted instantly.

Character classes

PatternMatchesExample
\dAny digit (0–9)\d+ matches "42" in "abc42"
\DAny non-digit\D+ matches "abc" in "abc42"
\wWord char: [a-zA-Z0-9_]\w+ matches "hello_world"
\WNon-word character\W matches spaces, punctuation
\sWhitespace (space, tab, newline)\s+ collapses multiple spaces
.Any character except newlinec.t matches "cat", "cut", "c4t"
[abc]a, b, or c[aeiou] matches vowels
[^abc]NOT a, b, or c[^0-9] matches non-digits
[a-z]Lowercase a through z[a-zA-Z] matches any letter

Quantifiers

QuantifierMeaningExample
*0 or moreab* matches "a", "ab", "abbb"
+1 or more\d+ matches one or more digits
?0 or 1 (optional)colou?r matches "color" and "colour"
{n}Exactly n times\d{4} matches exactly 4 digits
{n,}n or more times\d{3,} matches 3+ digits
{n,m}Between n and m times\d{2,4} matches 2, 3, or 4 digits

Anchors and boundaries

PatternMeaning
^Start of string (or line in multiline mode)
$End of string (or line in multiline mode)
\bWord boundary — between \w and \W
\BNon-word boundary

^\d+$ matches a string that contains only digits (nothing else). Without anchors, \d+ would match the digits inside any string, including strings with other characters.

Groups and alternation

(cat|dog)      # Matches "cat" or "dog"
(https?)       # Matches "http" or "https" (s is optional)
(?:abc)        # Non-capturing group — groups without creating a backreference
(\w+)@(\w+) # Capture groups — \1 = username, \2 = domain

Non-capturing groups (?:...)are useful when you need to group for alternation but don't need the matched text — they're slightly faster and don't pollute your backreference list.

The patterns you'll use most

# Email (basic — full RFC 5322 compliance is impractical)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

# URL
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b[-a-zA-Z0-9@:%_+.~#?&/=]*

# IPv4 address
^(\d{1,3}\.){3}\d{1,3}$

# Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

# US phone number
^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$

# Hex color
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

# Slug (URL-safe string)
^[a-z0-9]+(?:-[a-z0-9]+)*$

# Whitespace trimmer (replace with empty string)
^\s+|\s+$

# HTML tag stripper (remove all tags)
<[^>]*>

Flags that change matching behavior

  • i (case-insensitive): /hello/i matches "Hello", "HELLO", "hello"
  • g (global): Find all matches, not just the first one
  • m (multiline): ^ and $ match start/end of each line, not just the whole string
  • s (dotAll): . matches newlines too

Common mistakes

  • Forgetting to escape dots. . in regex means "any character." To match a literal dot, use \.. The regex thefreeaitools.com also matches "thefreeaitools_com" — use thefreeaitools\.com.
  • Greedy vs. lazy matching. <.+> is greedy — it matches from the first < to the LAST >. Use <.+?> for lazy matching (shortest possible match).
  • Catastrophic backtracking. Nested quantifiers like (a+)+ on a string that doesn't match can cause exponential backtracking and hang the browser. Avoid patterns with nested quantifiers on large inputs.

Related tools


Written by Achraf A., founder of TheFreeAITools.

Browse by category

Not sure which tool you need? Start with a category.

Everything you can do — for free

No software to buy. No account to create. Just open a tool and get it done.

Work with images

Compress photos before sending them by email, resize pictures for social media, remove backgrounds, or pick the perfect color for a design project — all without installing any app.

Edit and format text

Count words and characters in an essay, compare two documents side by side, convert text to different formats, or generate placeholder text for a presentation.

Stay safe online

Create a strong unique password in one click, check how secure a password is, encode or decode data, and generate secure tokens — your data never leaves your device.

Calculate anything

BMI, loan repayments, unit conversions, date differences, and dozens of other everyday calculations — no spreadsheet or formula knowledge required.

The Free AI Tools is a free collection of 221+ online tools that work directly in your web browser — no download, no installation, no account required. Whether you need to compress an image for email, count words in an essay, generate a strong password, create a QR code for your business, or format JSON for development — you will find a simple, free tool here.

Every tool is privacy-first: your files, text, and data never leave your device. Tools cover image editing, text processing, developer utilities, security & encoding, SEO & web, design & CSS, and more.

☕ Support Us