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
Start of string
^

# Match only at the beginning of the input.

End of string
$

# Match only at the end of the input.

Word boundary
\b

# Match a transition between word and non-word characters.

Non-word boundary
\B

# Match where a word boundary does not occur.

Absolute start anchor
\A

# Match only at the absolute start of the entire string.

Absolute end anchor
\Z

# Match only at the absolute end of the entire string.

## Character Classes
Digit shorthand
\d

# Match one ASCII digit character.

Non-digit shorthand
\D

# Match one non-digit character.

Word character
\w

# Match a word character.

Non-word character
\W

# Match a non-word character.

Whitespace shorthand
\s

# Match one whitespace character.

Non-whitespace shorthand
\S

# Match one non-whitespace character.

Character range
[a-z]

# Match one lowercase Latin letter.

Negated character class
[^0-9]

# Match one character not in the set.

Literal hyphen inside class
[-_]

# Match a hyphen or underscore.

Unicode letter property
\p{L}

# Match any Unicode letter.

Unicode number property
\p{N}

# Match any Unicode number.

## Quantifiers
Zero or more
ab*

# Repeat the previous token any number of times.

One or more
ab+

# Repeat the previous token at least once.

Zero or one
colou?r

# Make the previous token optional.

Exact repetition
\d{4}

# Match an exact number of repetitions.

Range repetition
\w{3,10}

# Match between a minimum and maximum number of repetitions.

Open-ended repetition
\d{2,}

# Match at least a minimum number of repetitions.

Lazy zero or more
<.*?>

# Use the shortest possible match.

Lazy one or more
".+?"

# Use the shortest possible `+` match.

Possessive quantifier
\d++

# Repeat without backtracking.

## Groups and Alternation
Capturing group
(\d{4})-(\d{2})-(\d{2})

# Capture a submatch for reuse or extraction.

Non-capturing group
(?:cat|dog)

# Group without creating a numbered capture.

Alternation
error|warning|info

# Match one branch or another.

Nested groups
^(?:https?|ftp)://([^/\s]+)

# Combine multiple grouped pieces.

Optional group
^(?:\+1[-.\s]?)?\d{3}[-.\s]?\d{3}[-.\s]?\d{4}$

# Make an entire grouped piece optional.

Branch reset group
(?|(Mr|Ms)\.?\s+(\w+)|(Dr)\.?\s+(\w+))

# Reset group numbers per alternation branch.

## Backreferences
Repeated word
\b(\w+)\s+\1\b

# Match the same word twice in a row.

Matching quote pair
(["\']).*?\1

# Require the closing quote to match the opener.

Same opening and closing tag
<([A-Za-z][A-Za-z0-9]*)\b[^>]*>.*?</\1>

# Require the same tag name at both ends.

Named backreference
(?<word>\w+)\s+\k<word>

# Reuse a named capture later in the pattern.

## Lookarounds
Positive lookahead
\w+(?=:)

# Require text to follow the current position.

Negative lookahead
foo(?!bar)

# Require text not to follow the current position.

Positive lookbehind
(?<=\$)\d+(?:\.\d{2})?

# Require text to precede the current position.

Negative lookbehind
(?<!\$)\b\d+\b

# Require text not to precede the current position.

Tempered dot
(?:(?!END).)+

# Match any text until a stopper without over-consuming.

## Flags and Modes
Ignore case
(?i)error

# Enable case-insensitive matching.

Multiline mode
(?m)^ERROR:

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

Dotall mode
(?s)<script.*?</script>

# Let dot match newlines.

Extended / verbose mode
(?x) ^ \d{4} - \d{2} - \d{2} $

# Allow whitespace and comments inside the regex.

Unicode-aware mode
(?u)\b\p{L}+\b

# Enable Unicode-aware parsing or matching.

Scoped flags
(?i:api)-v[0-9]+

# Enable or disable flags for only part of a pattern.

## Escaping and Literals
Literal dot
\.

# Match an actual period character.

Literal backslash
\\

# Match a backslash character.

Escaped metacharacters
[\^\$\.\|\?\*\+\(\)\{\}]

# Match literal regex metacharacters.

Quoted literal block
\Q[a-z]+(foo)?\E

# Treat a whole block as literal text.

## Performance and Backtracking
Atomic group
(?>\d{4}-\d{2}-\d{2})

# Prevent backtracking into a grouped subpattern.

Possessive class quantifier
[A-Z]++

# Consume as much as possible without backtracking.

Avoid nested greedy stars
(.*a)*

# Example of a risky pattern to rewrite.

Constrained tag content
<title>[^<]*</title>

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

Recommended next

No recommendations yet.