Regex Cheat Sheet

Core regular expression syntax, classes, quantifiers, groups, lookarounds, flags, and performance techniques.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Anchors and Boundaries

Match start/end positions and boundaries rather than literal characters.

Start of string

Match only at the beginning of the input.

regexANYanchorsboundariesstart
regex
^
Notes

Useful for line-based matching with multiline mode or full-string validation when combined with `$`.

End of string

Match only at the end of the input.

regexANYanchorsboundariesend
regex
$
Notes

Use with `^` for full-line or full-string matching.

Word boundary

Match a transition between word and non-word characters.

regexANYboundaryword
regex
\b
Notes

Common for matching whole words like `\berror\b`.

Non-word boundary

Match where a word boundary does not occur.

regexANYboundaryword
regex
\B
Notes

Useful for matching substrings that must stay inside a larger token.

Absolute start anchor

Match only at the absolute start of the entire string.

regexANYanchorspcre
regex
\A
Notes

Common in PCRE, Ruby, and Python-style engines; safer than `^` when multiline mode is enabled.

Absolute end anchor

Match only at the absolute end of the entire string.

regexANYanchorspcre
regex
\Z
Notes

Often used with `\A` for strict whole-string matches in PCRE-style engines.

Character Classes

Match one character from a set or shorthand class.

Digit shorthand

Match one ASCII digit character.

regexANYclassesdigits
regex
\d
Notes

Equivalent to `[0-9]` in many engines.

Non-digit shorthand

Match one non-digit character.

regexANYclassesdigits
regex
\D
Notes

Inverse of `\d`.

Word character

Match a word character.

regexANYclassesword
regex
\w
Notes

Usually letters, digits, and underscore; exact behavior varies with Unicode settings.

Non-word character

Match a non-word character.

regexANYclassesword
regex
\W
Notes

Inverse of `\w`.

Whitespace shorthand

Match one whitespace character.

regexANYclasseswhitespace
regex
\s
Notes

Often includes spaces, tabs, newlines, and other space-like code points.

Non-whitespace shorthand

Match one non-whitespace character.

regexANYclasseswhitespace
regex
\S
Notes

Inverse of `\s`.

Character range

Match one lowercase Latin letter.

regexANYclassesranges
regex
[a-z]
Notes

A range inside square brackets matches one character between the endpoints.

Negated character class

Match one character not in the set.

regexANYclassesnegation
regex
[^0-9]
Notes

A leading `^` inside `[]` negates the class.

Literal hyphen inside class

Match a hyphen or underscore.

regexANYclassesescaping
regex
[-_]
Notes

Put `-` first or last, or escape it, to avoid range behavior.

Unicode letter property

Match any Unicode letter.

regexANYclassesunicode
regex
\p{L}
Notes

Available in engines with Unicode property support.

Unicode number property

Match any Unicode number.

regexANYclassesunicode
regex
\p{N}
Notes

Useful for international numeric content beyond ASCII digits.

Quantifiers

Control how many times the previous token may repeat.

Zero or more

Repeat the previous token any number of times.

regexANYquantifiersgreedy
regex
ab*
Notes

Matches `a`, `ab`, `abb`, and so on.

One or more

Repeat the previous token at least once.

regexANYquantifiersgreedy
regex
ab+
Notes

Requires one or more `b` characters after `a`.

Zero or one

Make the previous token optional.

regexANYquantifiersoptional
regex
colou?r
Notes

Matches both `color` and `colour`.

Exact repetition

Match an exact number of repetitions.

regexANYquantifierscounts
regex
\d{4}
Notes

A common pattern for year-like numeric fields.

Range repetition

Match between a minimum and maximum number of repetitions.

regexANYquantifierscounts
regex
\w{3,10}
Notes

Useful for usernames and token length checks.

Open-ended repetition

Match at least a minimum number of repetitions.

regexANYquantifierscounts
regex
\d{2,}
Notes

Useful when only the lower bound matters.

Lazy zero or more

Use the shortest possible match.

regexANYquantifierslazy
regex
<.*?>
Notes

Common for tag-like extraction where greedy matching would overrun.

Lazy one or more

Use the shortest possible `+` match.

regexANYquantifierslazy
regex
".+?"
Notes

Useful for quoted strings without crossing too far.

Possessive quantifier

Repeat without backtracking.

regexANYquantifierspossessiveperformance
regex
\d++
Notes

Supported in PCRE/Java-style engines; useful to reduce catastrophic backtracking.

Groups and Alternation

Capture, group, and branch patterns.

Capturing group

Capture a submatch for reuse or extraction.

regexANYgroupscapture
regex
(\d{4})-(\d{2})-(\d{2})
Notes

Groups can be referenced later by number.

Non-capturing group

Group without creating a numbered capture.

regexANYgroupsnoncapturing
regex
(?:cat|dog)
Notes

Prefer when grouping is needed but capture data is not.

Alternation

Match one branch or another.

regexANYalternationbranches
regex
error|warning|info
Notes

Equivalent to logical OR across branches.

Nested groups

Combine multiple grouped pieces.

regexANYgroupsalternation
regex
^(?:https?|ftp)://([^/\s]+)
Notes

Captures the host while allowing several schemes.

Optional group

Make an entire grouped piece optional.

regexANYgroupsoptional
regex
^(?:\+1[-.\s]?)?\d{3}[-.\s]?\d{3}[-.\s]?\d{4}$
Notes

Common for optional prefixes in validators.

Branch reset group

Reset group numbers per alternation branch.

regexANYgroupspcreadvanced
regex
(?|(Mr|Ms)\.?\s+(\w+)|(Dr)\.?\s+(\w+))
Notes

PCRE feature useful when alternate branches should expose the same capture numbers.

Backreferences

Reuse earlier captures within the same regex.

Repeated word

Match the same word twice in a row.

regexANYbackreferencecleanup
regex
\b(\w+)\s+\1\b
Notes

Useful for finding duplicated words like `the the`.

Matching quote pair

Require the closing quote to match the opener.

regexANYbackreferencequotes
regex
(["\']).*?\1
Notes

Captures either single or double quote and requires the same closer.

Same opening and closing tag

Require the same tag name at both ends.

regexANYbackreferencetags
regex
<([A-Za-z][A-Za-z0-9]*)\b[^>]*>.*?</\1>
Notes

Works for simple tag-like text, though regex is not a full HTML parser.

Named backreference

Reuse a named capture later in the pattern.

regexANYbackreferencenamed
regex
(?<word>\w+)\s+\k<word>
Notes

Named syntax varies by engine but improves readability.

Lookarounds

Assert context without consuming characters.

Positive lookahead

Require text to follow the current position.

regexANYlookaroundlookahead
regex
\w+(?=:)
Notes

Matches the word before a colon without including the colon.

Negative lookahead

Require text not to follow the current position.

regexANYlookaroundlookahead
regex
foo(?!bar)
Notes

Matches `foo` not followed by `bar`.

Positive lookbehind

Require text to precede the current position.

regexANYlookaroundlookbehind
regex
(?<=\$)\d+(?:\.\d{2})?
Notes

Matches a currency amount after a dollar sign.

Negative lookbehind

Require text not to precede the current position.

regexANYlookaroundlookbehind
regex
(?<!\$)\b\d+\b
Notes

Matches bare numbers not immediately preceded by `$`.

Tempered dot

Match any text until a stopper without over-consuming.

regexANYlookaroundadvanced
regex
(?:(?!END).)+
Notes

Useful when you need to stop before a sentinel substring.

Flags and Modes

Inline and external flags that modify matching behavior.

Ignore case

Enable case-insensitive matching.

regexANYflagscase
regex
(?i)error
Notes

Equivalent to using an external ignore-case flag in many tools.

Multiline mode

Let `^` and `$` work per line.

regexANYflagsmultiline
regex
(?m)^ERROR:
Notes

Useful for log parsing.

Dotall mode

Let dot match newlines.

regexANYflagsdotall
regex
(?s)<script.*?</script>
Notes

Useful when matching across line breaks.

Extended / verbose mode

Allow whitespace and comments inside the regex.

regexANYflagsverbose
regex
(?x) ^ \d{4} - \d{2} - \d{2} $
Notes

Improves readability for long patterns.

Unicode-aware mode

Enable Unicode-aware parsing or matching.

regexANYflagsunicode
regex
(?u)\b\p{L}+\b
Notes

Exact syntax varies by engine, but the idea is to use Unicode classes safely.

Scoped flags

Enable or disable flags for only part of a pattern.

regexANYflagsscoped
regex
(?i:api)-v[0-9]+
Notes

Applies case-insensitive matching just to the group.

Escaping and Literals

Match literal symbols safely.

Literal dot

Match an actual period character.

regexANYescapingliterals
regex
\.
Notes

Dot is special outside a character class, so escape it when matching a literal `.`.

Literal backslash

Match a backslash character.

regexANYescapingliterals
regex
\\
Notes

Backslashes themselves must usually be escaped.

Escaped metacharacters

Match literal regex metacharacters.

regexANYescapingliterals
regex
[\^\$\.\|\?\*\+\(\)\{\}]
Notes

A class can list literal metacharacters more conveniently.

Quoted literal block

Treat a whole block as literal text.

regexANYescapingpcre
regex
\Q[a-z]+(foo)?\E
Notes

Supported in PCRE-style engines to avoid escaping many characters one by one.

Performance and Backtracking

Patterns that avoid pathological backtracking.

Atomic group

Prevent backtracking into a grouped subpattern.

regexANYperformanceatomic
regex
(?>\d{4}-\d{2}-\d{2})
Notes

Supported in PCRE/Java-style engines; useful in complex validators.

Possessive class quantifier

Consume as much as possible without backtracking.

regexANYperformancepossessive
regex
[A-Z]++
Notes

Can reduce backtracking in ambiguous patterns.

Avoid nested greedy stars

Example of a risky pattern to rewrite.

regexANYperformanceanti-pattern
regex
(.*a)*
Notes

Nested unlimited quantifiers can explode on some inputs. Prefer more precise tokens and anchors.

Constrained tag content

Use explicit stop characters instead of dot-star where possible.

regexANYperformanceprecision
regex
<title>[^<]*</title>
Notes

Usually faster and more predictable than `<title>.*?</title>`.

Recommended next

No recommendations yet.