Pattern language · quick reference
The Ultimate Regular Expressions Cheat Sheet
Find, validate, extract, and transform text with confidence. Choose a category below, then test your patterns against real examples.
All Tokens
Master referenceRegex flavors differ. JavaScript, PCRE, Python, .NET, Java, POSIX tools, and Vim do not support every token in exactly the same way. Always select the matching flavor when testing.
| Pattern | Meaning | Example | Matches |
|---|---|---|---|
. | Any character, usually except newline | c.t | cat, cot, c7t |
\ | Escape a special character | \. | A literal period |
[abc] | One character from the set | [cm]at | cat, mat |
[^abc] | One character not in the set | [^c]at | bat, not cat |
[a-z] | One character in a range | [a-f] | a through f |
\d / \D | Digit / non-digit | \d+ | 7, 2026 |
\w / \W | Word / non-word character | \w+ | user_42 |
\s / \S | Whitespace / non-whitespace | a\sb | a b |
^ / $ | Start / end of string or line | ^cat$ | Only cat |
\b / \B | Word boundary / non-boundary | \bcat\b | cat, not catalog |
* | Zero or more | ca*t | ct, cat, caaat |
+ | One or more | a+ | a, aa, aaa |
? | Zero or one | colou?r | color, colour |
{n} | Exactly n repetitions | a{2} | aa |
{n,m} | Between n and m repetitions | a{2,4} | aa, aaa, aaaa |
{n,} | At least n repetitions | a{2,} | aa, aaa... |
(ab) | Capturing group | (ab){2} | abab |
(?:ab) | Non-capturing group | (?:ha)+ | ha, haha |
cat|dog | Alternation: either pattern | cat|dog | cat or dog |
(?=x) / (?!x) | Positive / negative lookahead | \d+(?=px) | Digits before px |
(?<=x) / (?<!x) | Positive / negative lookbehind | (?<=\$)\d+ | Digits after $ |
\1 | Backreference to capture group 1 | (\w+)\s+\1 | go go |
Common Tokens
Everyday essentialsCharacters and sets
.any character except usually a newline\.literal period[abc]one listed character[^abc]one character not listed[a-z]one character in the range
Structure and repetition
^start and$end*zero or more+one or more?zero or one{2,4}between two and four
Shorthand classes
\ddigit\wword character\swhitespace- Uppercase forms—
\D,\W,\S—mean “not.”
Combining patterns
(...)capture a group(?:...)group without capturinga|bmatch a or b\bword boundary\1repeat captured group 1
General Tokens
Literals and control characters| Token | Meaning | Example | Matches |
|---|---|---|---|
abc | Literal sequence | cat | The exact text cat |
\n | Newline | one\ntwo | Text on consecutive lines |
\r | Carriage return | \r\n | Windows-style line ending |
\t | Tab | a\tb | a, tab, b |
\xHH | Character from hexadecimal byte | \x41 | A |
\uHHHH | Unicode code unit in supported engines | \u0041 | A |
\Q...\E | Treat content literally in supported engines | \Q$5.00\E | $5.00 |
(?#...) | Inline comment in supported engines | \d+(?# number) | A number |
Anchors
Match positions, not characters| Anchor | Meaning | Example | Result |
|---|---|---|---|
^ | Start of string, or line in multiline mode | ^Error | Lines beginning with Error |
$ | End of string, or line in multiline mode | done$ | Lines ending with done |
\A | Absolute start of string in supported engines | \AHello | Hello only at the beginning |
\Z | End of string, sometimes before final newline | world\Z | world at the end |
\z | Absolute end of string in supported engines | world\z | Strict end position |
\b | Word boundary | \bcat\b | cat, not catalog |
\B | Not a word boundary | \Bcat | cat inside another word |
\G | End of previous match or start position in supported engines | \G,?\w+ | Contiguous tokens |
Meta Sequences
Character shortcuts| Sequence | Meaning | Typical equivalent | Example |
|---|---|---|---|
\d | Digit | [0-9] in ASCII mode | \d{4} → 2026 |
\D | Non-digit | [^0-9] | \D+ → abc |
\w | Word character | Often [A-Za-z0-9_] | user_1 |
\W | Non-word character | Inverse of \w | !@# |
\s | Whitespace | Space, tab, newline | a\sb |
\S | Non-whitespace | Inverse of \s | hello |
\h / \H | Horizontal whitespace / inverse | Spaces and tabs | Flavor-dependent |
\v / \V | Vertical whitespace / inverse | Line-break characters | Flavor-dependent |
\R | Any Unicode line break | \n, \r\n, etc. | Flavor-dependent |
\p{L} | Any Unicode letter | Unicode property escape | A, é, 中 |
\p{N} | Any Unicode number | Unicode property escape | 4, ٢ |
\P{L} | Anything except a Unicode letter | Negated Unicode property | 7, ! |
Quantifiers
Control repetitionGreedy, lazy, and possessive: greedy quantifiers take as much as possible; lazy quantifiers prefer as little as possible; possessive quantifiers prevent backtracking and are not available in every engine.
| Quantifier | Meaning | Example | Matches |
|---|---|---|---|
* | Zero or more, greedy | go* | g, go, gooo |
+ | One or more, greedy | go+ | go, gooo |
? | Zero or one, greedy | go? | g, go |
{n} | Exactly n | \d{4} | 2026 |
{n,} | At least n | a{2,} | aa, aaa... |
{n,m} | From n through m | a{2,4} | aa through aaaa |
*? / +? | Lazy repetition | <.*?> | The shortest tag-like segment |
{n,m}? | Lazy bounded repetition | a{2,4}? | Prefers two as |
*+ / ++ | Possessive repetition in supported engines | a++a | Consumes without backtracking |
Group Constructs
Capture, branch, and assert| Construct | Meaning | Example | Result |
|---|---|---|---|
(abc) | Numbered capturing group | (ha)+ | ha, haha |
(?:abc) | Non-capturing group | (?:https?://)? | Optional protocol without a capture |
(?<name>abc) | Named capture in many engines | (?<year>\d{4}) | Capture named year |
(?P<name>abc) | Python-style named capture | (?P<year>\d{4}) | Capture named year |
a|b | Alternation | cat|dog | cat or dog |
\1 | Numbered backreference | (\w+)\s+\1 | A repeated word |
\k<name> | Named backreference in many engines | (?<w>\w+) \k<w> | A repeated named capture |
(?=abc) | Positive lookahead | \w+(?=:) | Word followed by a colon |
(?!abc) | Negative lookahead | foo(?!bar) | foo not followed by bar |
(?<=abc) | Positive lookbehind | (?<=\$)\d+ | Digits after a dollar sign |
(?<!abc) | Negative lookbehind | (?<!-)\d+ | Digits not preceded by a hyphen |
(?>abc) | Atomic group in supported engines | (?>a+)a | No backtracking inside the group |
(?i:abc) | Apply a modifier to one group | (?i:hello) | hello, HELLO |
Character Classes
Match one character from a set| Class | Meaning | Example | Matches |
|---|---|---|---|
[abc] | Any one listed character | [abc] | a, b, or c |
[^abc] | Any character except those listed | [^0-9] | One non-digit |
[a-z] | Lowercase ASCII range | [a-z]+ | hello |
[A-Z] | Uppercase ASCII range | [A-Z]{2} | US |
[0-9] | ASCII digit range | [0-9]{2} | 42 |
[A-Za-z0-9_] | ASCII word-style character | [A-Za-z_]\w* | A simple identifier |
[a-fA-F0-9] | Hexadecimal digit | #[a-fA-F0-9]{6} | #00bcd4 |
[._-] | Period, underscore, or hyphen | [._-] | One listed symbol |
[\[\]] | Opening or closing square bracket | [\[\]] | [ or ] |
[[:digit:]] | POSIX digit class | [[:digit:]]+ | Digits in supporting engines |
[[:space:]] | POSIX whitespace class | [[:space:]]+ | Whitespace in supporting engines |
Remember:
[cat] matches one character—c, a, or t. Use (cat) to group the complete word. Place - first or last, or escape it, when you need a literal hyphen.Flags / Modifiers
Change matching behavior| Flag | Name | Effect | Example |
|---|---|---|---|
i | Case-insensitive | Ignore letter case | /cat/i matches CAT |
g | Global | Find or replace all matches | /cat/g |
m | Multiline | ^ and $ match line boundaries | /^Error/gm |
s | Dotall / single-line | . also matches newlines | /start.*end/s |
u | Unicode | Enable Unicode-aware behavior in engines such as JavaScript | /\p{L}+/u |
x | Extended / free-spacing | Allow layout whitespace and comments in supported engines | (?x) \d+ \s+ \w+ |
y | Sticky | Match only at the current position in JavaScript | /\w+/y |
d | Indices | Return match indices in modern JavaScript | /cat/d |
Inline modifiers
(?i)cat enables case-insensitive matching where supported. (?i:cat) limits the modifier to one group.
Vim modifiers
Use \c for case-insensitive and \C for case-sensitive matching, or configure :set ignorecase.
Substitutions
Search and replaceReplacement syntax varies. JavaScript commonly uses
$1, Python uses \g<1>, and Vim commonly uses \1. Confirm the syntax for your tool.| Goal | Search | Replacement | Result |
|---|---|---|---|
| Swap first and last names | (\w+)\s+(\w+) | $2, $1 | Ada Lovelace → Lovelace, Ada |
| Reformat ISO date | (\d{4})-(\d{2})-(\d{2}) | $3/$2/$1 | 2026-07-31 → 31/07/2026 |
| Collapse whitespace | \s+ | One space | too many → too many |
| Remove Markdown checkboxes | \[[ xX]\] | Empty text | Removes [ ], [x], and [X] |
| Wrap every number | (\d+) | [$1] | 42 → [42] |
Common replacement references
$&entire match in JavaScript$1,$2numbered groups in JavaScript${name}named group in JavaScript\g<name>named group in Python
Vim substitution
:%s/old/new/greplace all occurrences:%s/\[x\]//gdelete every[x]:%s/\[[ xX]\]//gcdelete checkboxes with confirmation&entire match;\1captured group 1