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 reference
i
Regex 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.
PatternMeaningExampleMatches
.Any character, usually except newlinec.tcat, cot, c7t
\Escape a special character\.A literal period
[abc]One character from the set[cm]atcat, mat
[^abc]One character not in the set[^c]atbat, not cat
[a-z]One character in a range[a-f]a through f
\d / \DDigit / non-digit\d+7, 2026
\w / \WWord / non-word character\w+user_42
\s / \SWhitespace / non-whitespacea\sba b
^ / $Start / end of string or line^cat$Only cat
\b / \BWord boundary / non-boundary\bcat\bcat, not catalog
*Zero or moreca*tct, cat, caaat
+One or morea+a, aa, aaa
?Zero or onecolou?rcolor, colour
{n}Exactly n repetitionsa{2}aa
{n,m}Between n and m repetitionsa{2,4}aa, aaa, aaaa
{n,}At least n repetitionsa{2,}aa, aaa...
(ab)Capturing group(ab){2}abab
(?:ab)Non-capturing group(?:ha)+ha, haha
cat|dogAlternation: either patterncat|dogcat or dog
(?=x) / (?!x)Positive / negative lookahead\d+(?=px)Digits before px
(?<=x) / (?<!x)Positive / negative lookbehind(?<=\$)\d+Digits after $
\1Backreference to capture group 1(\w+)\s+\1go go

Common Tokens

Everyday essentials

Characters 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

  • \d digit
  • \w word character
  • \s whitespace
  • Uppercase forms—\D, \W, \S—mean “not.”

Combining patterns

  • (...) capture a group
  • (?:...) group without capturing
  • a|b match a or b
  • \b word boundary
  • \1 repeat captured group 1

General Tokens

Literals and control characters
TokenMeaningExampleMatches
abcLiteral sequencecatThe exact text cat
\nNewlineone\ntwoText on consecutive lines
\rCarriage return\r\nWindows-style line ending
\tTaba\tba, tab, b
\xHHCharacter from hexadecimal byte\x41A
\uHHHHUnicode code unit in supported engines\u0041A
\Q...\ETreat 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
AnchorMeaningExampleResult
^Start of string, or line in multiline mode^ErrorLines beginning with Error
$End of string, or line in multiline modedone$Lines ending with done
\AAbsolute start of string in supported engines\AHelloHello only at the beginning
\ZEnd of string, sometimes before final newlineworld\Zworld at the end
\zAbsolute end of string in supported enginesworld\zStrict end position
\bWord boundary\bcat\bcat, not catalog
\BNot a word boundary\Bcatcat inside another word
\GEnd of previous match or start position in supported engines\G,?\w+Contiguous tokens

Meta Sequences

Character shortcuts
SequenceMeaningTypical equivalentExample
\dDigit[0-9] in ASCII mode\d{4}2026
\DNon-digit[^0-9]\D+abc
\wWord characterOften [A-Za-z0-9_]user_1
\WNon-word characterInverse of \w!@#
\sWhitespaceSpace, tab, newlinea\sb
\SNon-whitespaceInverse of \shello
\h / \HHorizontal whitespace / inverseSpaces and tabsFlavor-dependent
\v / \VVertical whitespace / inverseLine-break charactersFlavor-dependent
\RAny Unicode line break\n, \r\n, etc.Flavor-dependent
\p{L}Any Unicode letterUnicode property escapeA, é,
\p{N}Any Unicode numberUnicode property escape4, ٢
\P{L}Anything except a Unicode letterNegated Unicode property7, !

Quantifiers

Control repetition
Q
Greedy, 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.
QuantifierMeaningExampleMatches
*Zero or more, greedygo*g, go, gooo
+One or more, greedygo+go, gooo
?Zero or one, greedygo?g, go
{n}Exactly n\d{4}2026
{n,}At least na{2,}aa, aaa...
{n,m}From n through ma{2,4}aa through aaaa
*? / +?Lazy repetition<.*?>The shortest tag-like segment
{n,m}?Lazy bounded repetitiona{2,4}?Prefers two as
*+ / ++Possessive repetition in supported enginesa++aConsumes without backtracking

Group Constructs

Capture, branch, and assert
ConstructMeaningExampleResult
(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|bAlternationcat|dogcat or dog
\1Numbered backreference(\w+)\s+\1A 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 lookaheadfoo(?!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+)aNo backtracking inside the group
(?i:abc)Apply a modifier to one group(?i:hello)hello, HELLO

Character Classes

Match one character from a set
ClassMeaningExampleMatches
[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
FlagNameEffectExample
iCase-insensitiveIgnore letter case/cat/i matches CAT
gGlobalFind or replace all matches/cat/g
mMultiline^ and $ match line boundaries/^Error/gm
sDotall / single-line. also matches newlines/start.*end/s
uUnicodeEnable Unicode-aware behavior in engines such as JavaScript/\p{L}+/u
xExtended / free-spacingAllow layout whitespace and comments in supported engines(?x) \d+ \s+ \w+
yStickyMatch only at the current position in JavaScript/\w+/y
dIndicesReturn 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 replace
S
Replacement syntax varies. JavaScript commonly uses $1, Python uses \g<1>, and Vim commonly uses \1. Confirm the syntax for your tool.
GoalSearchReplacementResult
Swap first and last names(\w+)\s+(\w+)$2, $1Ada LovelaceLovelace, Ada
Reformat ISO date(\d{4})-(\d{2})-(\d{2})$3/$2/$12026-07-3131/07/2026
Collapse whitespace\s+One spacetoo   manytoo many
Remove Markdown checkboxes\[[ xX]\]Empty textRemoves [ ], [x], and [X]
Wrap every number(\d+)[$1]42[42]

Common replacement references

  • $& entire match in JavaScript
  • $1, $2 numbered groups in JavaScript
  • ${name} named group in JavaScript
  • \g<name> named group in Python

Vim substitution

  • :%s/old/new/g replace all occurrences
  • :%s/\[x\]//g delete every [x]
  • :%s/\[[ xX]\]//gc delete checkboxes with confirmation
  • & entire match; \1 captured group 1
Build patterns one token at a time, test positive and negative cases, and practice interactively at regex101.com.