Regex Cheatsheet

A comprehensive reference guide for regular expressions. Use this to quickly look up syntax, character classes, and complex groupings.

Character Classes

.

Any character

Matches any character except line breaks.

\w

Word

Matches any word character (alphanumeric & underscore).

\W

Not word

Matches any non-word character.

\d

Digit

Matches any digit character (0-9).

\D

Not digit

Matches any non-digit character.

\s

Whitespace

Matches any whitespace character (spaces, tabs, line breaks).

\S

Not whitespace

Matches any non-whitespace character.

Anchors

^

Beginning

Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled.

$

End

Matches the end of the string, or the end of a line if the multiline flag (m) is enabled.

\b

Word boundary

Matches a word boundary position between a word character and non-word character or position (start / end of string).

\B

Not word boundary

Matches any position that is not a word boundary.

Groups & Lookarounds

(abc)

Capture group

Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference.

(?:abc)

Non-capturing group

Groups multiple tokens together without creating a capture group.

(?<name>abc)

Named capture group

Creates a capturing group that can be referenced via the specified name.

(?=abc)

Positive lookahead

Matches a group after the main expression without including it in the result.

(?!abc)

Negative lookahead

Specifies a group that can not match after the main expression.

(?<=abc)

Positive lookbehind

Matches a group before the main expression without including it in the result.

(?<!abc)

Negative lookbehind

Specifies a group that can not match before the main expression.

Quantifiers

+

Plus

Matches 1 or more of the preceding token.

*

Star

Matches 0 or more of the preceding token.

{1,3}

Quantifier

Matches the specified quantity of the previous token. {1,3} will match 1 to 3. {3} will match exactly 3. {3,} will match 3 or more.

?

Optional

Matches 0 or 1 of the preceding token, effectively making it optional.

+?

Lazy

Makes the preceding quantifier lazy, causing it to match as few characters as possible.