← Back to all tools

📖 Regex Cheat Sheet

Complete regular expression reference with quick tester

Quick Tester

Character Classes

PatternDescriptionExample
.Any character except newlinea.c → abc, a1c
\dDigit [0-9]\d{3} → 123
\DNot a digit\D+ → hello
\wWord char [a-zA-Z0-9_]\w+ → hello_42
\WNot a word character\W → @, !
\sWhitespacea\sb → a b
\SNot whitespace
[abc]Character set (a, b, or c)[aeiou]
[^abc]Negated set (not a, b, c)[^0-9]
[a-z]Range[A-Za-z]

Quantifiers

PatternDescription
*0 or more
+1 or more
?0 or 1 (optional)
{n}Exactly n times
{n,}n or more
{n,m}Between n and m
*? +?Lazy (non-greedy) versions

Anchors & Boundaries

PatternDescription
^Start of string/line
$End of string/line
\bWord boundary
\BNot a word boundary

Groups & Lookaround

PatternDescription
(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named group
\1Backreference to group 1
a|bAlternation (a or b)
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind

Flags

FlagDescription
gGlobal — find all matches
iCase-insensitive
mMultiline (^ and $ match line boundaries)
sDotall (. matches newlines)
uUnicode support

Common Patterns

Use CasePattern
Email[\w.-]+@[\w.-]+\.\w{2,}
URLhttps?://[\w.-]+(?:/[\w./?%&=-]*)?
Phone (US)\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
IP Address\b\d{1,3}(\.\d{1,3}){3}\b
Date (YYYY-MM-DD)\d{4}-\d{2}-\d{2}
Hex Color#[0-9a-fA-F]{3,8}
HTML Tag<[^>]+>

About Regex Cheat Sheet

A comprehensive regular expression reference covering character classes, quantifiers, anchors, groups, lookaheads, flags, and common patterns. Includes a quick inline tester. Bookmark this page for quick regex reference while coding.

How to Use Regex Cheat Sheet

  1. Browse regex patterns by category (anchors, quantifiers, groups, etc.)
  2. Click any pattern to copy it
  3. Use the search to find patterns by name or description
  4. View examples showing what each pattern matches
  5. Combine patterns to build complex expressions

About Regex Cheat Sheet

Regular expressions are the Swiss Army knife of text processing — powerful but notoriously hard to remember. This cheat sheet organizes every common regex pattern into logical categories with clear explanations and examples. Whether you're matching email addresses, parsing log files, validating input, or doing find-and-replace in your IDE, having a quick reference saves time and frustration. Patterns are organized by type: character classes (\d, \w, \s), quantifiers (*, +, ?, {n}), anchors (^, $, \b), groups and lookaheads, and common real-world patterns. Each entry includes the syntax, what it matches, and a practical example. Bookmark this page — you'll come back to it more often than you think.

Frequently Asked Questions

What does the 'g' flag do?

The global flag 'g' finds all matches in the string, not just the first one. Without 'g', regex stops after the first match. Other common flags: 'i' (case-insensitive), 'm' (multiline), 's' (dotAll).

What's the difference between .* and .*? ?

'.*' is greedy — it matches as much as possible. '.*?' is lazy — it matches as little as possible. For 'hello world', greedy '.*' matches everything; lazy '.*?' matches each tag separately.

How do I match a literal dot or bracket?

Escape special characters with a backslash: \. matches a literal dot, \[ matches a literal bracket. Inside a character class [.] the dot is already literal. The special characters that need escaping are: . * + ? ^ $ { } [ ] ( ) | \.

What are lookaheads and lookbehinds?

Lookahead (?=...) matches a position followed by the pattern. Lookbehind (?<=...) matches a position preceded by the pattern. They don't consume characters — they just check if the pattern exists. 'foo(?=bar)' matches 'foo' only when followed by 'bar'.

Is regex the same in all programming languages?

The core syntax is similar but flavors differ. JavaScript lacks lookbehinds in older engines and atomic groups. Python supports named groups (?P). PCRE (PHP, Perl) has the most features. POSIX regex (grep) uses different syntax for some constructs.

Related Tools