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
| Pattern | Matches | Example |
|---|---|---|
. | Any character except newline | h.t β hat, hot, hit |
\d | Any digit [0-9] | \d{3} β 123, 456 |
\D | Any non-digit | \D+ β abc, xyz |
\w | Word character [a-zA-Z0-9_] | \w+ β hello_world |
\W | Non-word character | \W β @, #, space |
\s | Whitespace (space, tab, newline) | \s+ β spaces |
\S | Non-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
| Quantifier | Meaning | Example |
|---|---|---|
* | 0 or more | ab*c β ac, abc, abbc |
+ | 1 or more | ab+c β abc, abbc (not ac) |
? | 0 or 1 | colou?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
| Anchor | Position |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
\B | Not a word boundary |
\bcat\b matches "cat" in "the cat sat" but not in "category" or "concatenate".
Groups and Capturing
| Syntax | Purpose |
|---|---|
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
\1 | Backreference to group 1 |
a|b | Alternation (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
| Syntax | Name | Example |
|---|---|---|
(?=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
| Flag | Name | Effect |
|---|---|---|
g | Global | Match all occurrences, not just first |
i | Case-insensitive | /hello/i matches Hello, HELLO |
m | Multiline | ^ and $ match line starts/ends |
s | Dotall | . matches newlines too |
u | Unicode | Full Unicode support |
Common Patterns
| Pattern | Regex |
|---|---|
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ | |
| URL | https?://[\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.