Skip to content

Regex Cheat Sheet

Matching Characters

[abc] Matches any single character: a, b, or c
[^abc] Matches any single character except: a, b, or c. “^” is only valid as the first character
[a-g] Matches any single character in the range a-g
[^a-g] Matches any single character NOT in the range a-g
[H-N] Matches any single character in the range H-N
[0-9] Matches any single digit in the range 0-9
[a-gH-N] Matches any single character in the range a-g or H-N
Wildcards:
. [^\n\r], matches any character except newline
\s whitespace character, i.e., newline, tab, space, etc.
\S [^\s], any non-whitespace character
\d digit: [0-9]
\D [^\d], any non-digit character
\w word character: [0-9A-Za-z_]
\W [^\w], any non-word character

Matching Groups

(a b)
(…) Capturing group
(?:…) Non-capturing group
(?…) or (?’name’…) Named capturing group (name can be customized)
(?(condition)true_regex false_regex)

group(0) is used to get the entire match result. group(>0) can get sub-capturing groups from the entire match result.

Note: Named capturing groups are supported in Java 1.7+ (using the group method with name as parameter). JavaScript and Python temporarily do not support named capturing groups in some environments (though modern versions do).

Frequency Range

{3} Preceding item appears exactly 3 times, equivalent to {3,3}
{3,6} Preceding item appears between 3 and 6 times
{3,} Preceding item appears at least 3 times
{0,6} Preceding item appears at most 6 times
Wildcards:
* {0,} Zero or more
+ {1,} One or more
? {0,1} Zero or one
\w* Greedy mode
\w*? Non-greedy (lazy) mode

Anchors

^ Start of string
$ End of string
\b Boundary, word boundary
\B Non-word boundary
Lookaround Assertions:
(?=exp) Positive lookahead (suffix is exp)
(?<=exp) Positive lookbehind (prefix is exp)
(?!exp) Negative lookahead (suffix is NOT exp)
(?<!exp) Negative lookbehind (prefix is NOT exp)

\b(\w+)\b is equivalent to (?<=\W?)(\w+)(?=\W?)