Regex cheat sheet

Every common regular expression token on one page. Try any of them live in the regex tester.

Characters

TokenMeaning
.Any character except newline
\dDigit 0–9
\DNot a digit
\wWord character [A-Za-z0-9_]
\WNot a word character
\sWhitespace (space, tab, newline…)
\SNot whitespace
\t \n \rTab, newline, carriage return
\uFFFFUnicode code unit
\p{L}Any letter (needs u flag)

Sets

TokenMeaning
[abc]a, b or c
[^abc]Anything but a, b, c
[a-z]Range a to z
[a-zA-Z0-9]Alphanumeric

Anchors

TokenMeaning
^Start of string (line with m)
$End of string (line with m)
\bWord boundary
\BNot a word boundary

Quantifiers

TokenMeaning
*0 or more
+1 or more
?0 or 1
{3}Exactly 3
{2,}2 or more
{2,5}2 to 5
*? +? ??Lazy (as few as possible)

Groups

TokenMeaning
(abc)Capture group
(?:abc)Non-capturing group
(?<name>abc)Named group
\1Back-reference to group 1
\k<name>Back-reference by name
a|ba or b

Lookaround

TokenMeaning
(?=x)Followed by x
(?!x)Not followed by x
(?<=x)Preceded by x
(?<!x)Not preceded by x

Replacement

TokenMeaning
$1Group 1
$<name>Named group
$&Whole match
$`Text before match
$'Text after match
$$Literal $

Worked example

To capture the year, month and day from 2026-11-26 use (\d{4})-(\d{2})-(\d{2}). Replacing with $3/$2/$1 produces 26/11/2026.

Frequently asked questions

What is the difference between * and +?
* allows zero occurrences, + requires at least one.
What does a lazy quantifier do?
It matches as little as possible. On <b>x</b>, <.+?> matches <b> while <.+> matches the whole string.