Regex Cheat Sheet: Common Patterns & Quick Reference

Β· 10 min read

Regex Basics

Regular expressions (regex) are patterns used to match character combinations in strings. They are supported in virtually every programming language and many text editors. A regex pattern is composed of literal characters and special metacharacters that define matching rules.

The simplest regex is a literal string: hello matches the text "hello" exactly. The power comes from metacharacters that add flexibility. Use our Regex Tester to experiment with patterns in real time.

Character Classes

PatternMatchesExample
.Any character except newlineh.t β†’ hat, hot, hit
\dAny digit [0-9]\d{3} β†’ 123, 456
\DAny non-digit\D+ β†’ abc, xyz
\wWord character [a-zA-Z0-9_]\w+ β†’ hello_world
\WNon-word character\W β†’ @, #, space
\sWhitespace (space, tab, newline)\s+ β†’ spaces
\SNon-whitespace\S+ β†’ words
[abc]Any of a, b, or c[aeiou] β†’ vowels
[^abc]Not a, b, or c[^0-9] β†’ non-digits
[a-z]Range: a through z[A-Za-z] β†’ letters

Quantifiers

QuantifierMeaningExample
*0 or moreab*c β†’ ac, abc, abbc
+1 or moreab+c β†’ abc, abbc (not ac)
?0 or 1colou?r β†’ color, colour
{n}Exactly n\d{4} β†’ 2026
{n,}n or more\w{3,} β†’ 3+ char words
{n,m}Between n and m\d{2,4} β†’ 12, 123, 1234
*?Lazy (minimal match)<.*?> β†’ first tag only

Greedy vs Lazy: By default, quantifiers are greedy (match as much as possible). Adding ? makes them lazy (match as little as possible). For <b>bold</b>, <.*> matches the entire string, while <.*?> matches just <b>.

Anchors

AnchorPosition
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNot a word boundary

\bcat\b matches "cat" in "the cat sat" but not in "category" or "concatenate".

Groups and Capturing

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

Example: (\w+)\s\1 matches repeated words like "the the" or "is is". Use Find and Replace with regex to fix such duplicates.

Lookahead and Lookbehind

SyntaxNameExample
(?=abc)Positive lookahead\d(?=px) β†’ digit before "px"
(?!abc)Negative lookahead\d(?!px) β†’ digit not before "px"
(?<=abc)Positive lookbehind(?<=\$)\d+ β†’ digits after $
(?<!abc)Negative lookbehind(?<!\$)\d+ β†’ digits not after $

Flags

FlagNameEffect
gGlobalMatch all occurrences, not just first
iCase-insensitive/hello/i matches Hello, HELLO
mMultiline^ and $ match line starts/ends
sDotall. matches newlines too
uUnicodeFull Unicode support

Common Patterns

PatternRegex
Email^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
URLhttps?://[\w.-]+(?:\.[\w]{2,})(?:/[\w./?%&=-]*)?
Phone (US)\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
IPv4\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])
Hex Color#(?:[0-9a-fA-F]{3}){1,2}\b
Username^[a-zA-Z][a-zA-Z0-9_-]{2,15}$
ZIP Code (US)\b\d{5}(?:-\d{4})?\b

Use Extract Emails and Extract URLs tools for quick extraction without writing regex.

Regex in Different Languages

JavaScript

const pattern = /\d+/g;
const matches = "abc 123 def 456".match(pattern); // ["123", "456"]
"hello".replace(/l/g, "r"); // "herro"

Python

import re
matches = re.findall(r'\d+', 'abc 123 def 456')  # ['123', '456']
re.sub(r'l', 'r', 'hello')  # 'herro'

CLI (grep/sed)

grep -E '\d{3}-\d{4}' contacts.txt
sed -E 's/[0-9]+/NUM/g' data.txt

Frequently Asked Questions

What does \b mean in regex?

\b is a word boundary anchor. It matches the position between a word character and a non-word character. \bcat\b matches "cat" but not "category".

What is the difference between .* and .*?

.* is greedy (matches as much as possible). .*? is lazy (matches as little as possible).

How do I match a literal dot?

Escape it with backslash: \. Without escaping, dot matches any character.

What are non-capturing groups?

(?:...) groups patterns without creating a backreference, useful when you need grouping but don't need to capture.

How do I validate an email with regex?

Use: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ β€” covers most valid emails.

Related Tools

Regex Tester Find and Replace Extract Emails Extract URLs