Regex Tester

Jump to Regex Cheat Sheet

Regex Cheat Sheet

This cheat sheet summarizes common regex constructs:

Regex Description Example
. Matches any character except newline. a.c matches "abc", "a-c", etc.
\d Matches any digit (0-9). \d\d matches "42".
\D Matches any non-digit character. \D\D matches "ab".
\w Matches any word character (letter, digit, underscore). \w+ matches "Hello123".
\W Matches any non-word character. \W matches "@" in an email.
\s Matches any whitespace character. \s matches a space.
\S Matches any non-whitespace character. \S+ matches "Hello".
^ Matches the start of the string. ^Hello matches "Hello" at the beginning.
$ Matches the end of the string. end$ matches "end" at the end.
* Matches 0 or more occurrences of the preceding element. ab* matches "a", "ab", "abb", etc.
+ Matches 1 or more occurrences of the preceding element. ab+ matches "ab", "abb", etc.
? Matches 0 or 1 occurrence; also used for non-greedy quantifiers. colou?r matches both "color" and "colour".
{n} Matches exactly n occurrences. \d{3} matches "123".
{n,} Matches n or more occurrences. \d{2,} matches "12", "123", etc.
{n,m} Matches between n and m occurrences. \d{2,4} matches "12", "123", or "1234".
[abc] Matches any one character in the set. [aeiou] matches any vowel.
(x|y) Matches either x or y (alternation). cat|dog matches "cat" or "dog".
() Groups sub-patterns and captures the match. (abc)+ matches "abc", "abcabc", etc.
(?:) Groups sub-patterns without capturing. (?:abc)+ works like a capturing group but does not store the match.
\ Escapes a special character, making it literal. \. matches a literal dot.

Use this cheat sheet as a reference when crafting your regex patterns.