Regular Expression Tools

Universal Regex Cheat Sheet

Basic Patterns

Pattern Description
^ Matches the beginning of a string.
$ Matches the end of a string.
. Matches any single character except newline characters.
[...] Matches any character listed between the square brackets.
[^...] Matches any character not listed between the square brackets.
\d Matches any digit.
\D Matches any non-digit.
\w Matches any word character (alphanumeric plus underscore).
\W Matches any non-word character.
\s Matches any whitespace character.
\S Matches any non-whitespace character.
\b Matches a word boundary.
\B Matches a non-word boundary.

Quantifiers

Pattern Description
* Matches zero or more occurrences of the preceding element.
+ Matches one or more occurrences of the preceding element.
? Matches zero or one occurrence of the preceding element.
{n} Matches exactly n occurrences of the preceding element.
{n,} Matches n or more occurrences of the preceding element.
{n,m} Matches between n and m occurrences of the preceding element.

Groups and Lookaround

Pattern Description
(...) Defines a capturing group.
(?:...) Defines a non-capturing group.
(?=...) Positive lookahead assertion.
(?!...) Negative lookahead assertion.
(?<=...) Positive lookbehind assertion (not supported in all languages).
(? Negative lookbehind assertion (not supported in all languages).

Common Escape Sequences

Pattern Description
\ Escapes a special character.
\t Matches a tab character.
\n Matches a newline character.
\r Matches a carriage return character.

Character Classes

Pattern Description
\a Matches an alarm/bell character (ASCII 7).
\e Matches an escape character (ASCII 27), not universally supported.
\f Matches a form feed character (ASCII 12).
\v Matches a vertical tab character (ASCII 11).
\cX Matches a control character, where X is the letter of the control character.

Backreferences

Pattern Description
\1, \2, ... Backreference to a previously captured group. The number corresponds to the group number.

Modifiers (Flags)

Modifier Description
g Global search. Don't return after the first match.
u Full Unicode matching. Treat the pattern as a sequence of Unicode code points.

POSIX Character Classes

Pattern Description
[:punct:] Matches punctuation characters.
[:cntrl:] Matches control characters.
[:graph:] Matches characters that have a visual representation (not including space).
[:print:] Matches characters that have a visual representation (including space).
[:xdigit:] Matches hexadecimal digits.

Atomic Groups (Non-backtracking)

Pattern Description
(?>...) Atomic group. Once a sub-pattern has matched, the regex engine won't backtrack beyond that point.

Boundary Matchers

Pattern Description
\G Matches the position where the previous match ended.
\z Matches the absolute end of the input.
\Z Matches the end of the input but before the final line terminator, if any.

Unicode Property Escapes

Pattern Description
\p{Property} Matches a character with a particular Unicode property, e.g., \p{L} for a letter. Not universally supported.
\P{Property} Matches a character without a particular Unicode property. Not universally supported.

Mode Modifiers (Inline Flags)

Pattern Description
(?i) Case-insensitive mode.
(?m) Multiline mode. Changes the behavior of ^ and $.
(?s) Dot matches newline mode.
(?x) Extended mode. Ignores whitespace and allows comments.

Comments in Regex

Pattern Description
(?#...) Comment. The content inside is ignored by the regex engine.
Copyright © 2019 - RegExpLib.com