Regular Expressions Cheatsheet

Literals and Character Classes

Use this Regular Expressions reference while you build software engineering projects, review code, or refresh the syntax you reach for most.

Metacharacters

Twelve characters carry special meaning outside a bracket expression. Everything else matches itself.

CharMeaningLiteral form
.any char except newline (unless s flag)\.
^start of string or line\^
$end of string or line\$
*0 or more\*
+1 or more\+
?0 or 1, or lazy modifier\?
[ ]character class\[ \]
( )group\( \)
{ }counted quantifier\{ \}
|alternation\|
\escape\\

Inside [...] the rules change. Only ^ (first position), ], -, and \ are special, so [.*+] matches a literal dot, star, or plus.

Character classes

A bracket expression matches exactly one character from the set.

PatternMatches
[abc]a, b, or c
[^abc]any one char that is not a, b, c
[a-z]one lowercase ASCII letter
[a-zA-Z0-9_]one word char, ASCII
[0-9a-fA-F]one hex digit
[]a]] or a (POSIX: ] first is literal)
[a\]b]a, ], or b (escape form, most flavors)
[-a-z]- or a to z (dash first is literal)
[a-z-]dash last is also literal

[^abc] matches a newline in most flavors, which is the classic surprise when you expect it to stay on one line. Use [^abc\n] when you mean it.

Shorthand classes

ShorthandEquivalent (ASCII)Negated
\d[0-9]\D
\w[A-Za-z0-9_]\W
\s[ \t\r\n\f\v]\S
\hhorizontal space (PCRE, Ruby)\H
\Rany line break, \r\n as one unit (PCRE)—

Shorthands work inside a class too, so [\d.] is a digit or a dot and [\w-] is a word char or a hyphen. [\d\s] is a union, not an intersection.

Unicode caveat. In Python 3 \d and \w are Unicode-aware by default, so \d matches Arabic-Indic digits and \w matches accented letters. Pass re.ASCII to restrict them. In JavaScript \d is always [0-9], and \w is always ASCII, even with the u flag.

POSIX bracket expressions

Only usable inside a class, so the outer brackets are required: [[:digit:]], not [:digit:].

ClassMeaning
[:alpha:]letters
[:digit:]digits
[:alnum:]letters and digits
[:space:]whitespace
[:upper:] [:lower:]case
[:punct:]punctuation
[:xdigit:]hex digits
[:word:]alnum plus underscore (GNU)
[:blank:]space and tab only

Supported by grep, sed, PCRE, Ruby, and Java. Not supported by JavaScript or Python re.

# strip everything that is not a word char or space
echo "a-b_c 1!" | sed -E 's/[^[:alnum:]_ ]//g'
# -> ab_c 1

Escapes for non-printing characters

EscapeChar
\nnewline, U+000A
\rcarriage return, U+000D
\ttab
\f \vform feed, vertical tab
\0NUL (careful, in some flavors \0 is a backreference)
\xHH2 hex digits, \x41 is A
\x{1F600}code point, PCRE, Java, Ruby
\u{1F600}code point, JS with u or v (not \x{...})
\uHHHH4 hex digits, JS, Java, .NET
\cXcontrol char, \cJ is newline
\Q...\Equote everything between, PCRE and Java

\Q...\E is the fast way to match a literal string that came from user input in Java or PCRE. Elsewhere use the host language: re.escape() in Python, regexp.QuoteMeta() in Go, and in JavaScript RegExp.escape() where available, otherwise s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").