/regex-cheatsheetRegex Cheatsheet
Quick reference for regular expression syntax and patterns
.Any character excluding a newline or carriage return
[A-Za-z]Alphabet
[a-z]Lowercase alphabet
[A-Z]Uppercase alphabet
\dDigit [0-9]
\DNon-digit [^0-9]
_Underscore
\wAlphabet, digit or underscore [A-Za-z0-9_]
\WInverse of \w [^A-Za-z0-9_]
\SInverse of \s
Space
\tTab
\nNewline
\rCarriage return
\sSpace, tab, newline or carriage return
[xyz]Either x, y or z
[^xyz]Neither x, y nor z
[1-3]Either 1, 2 or 3
[^1-3]Neither 1, 2 nor 3
\.Period (outside character set)
matches literal dot
\^Caret (outside character set)
escaped ^
\$Dollar sign
\$ matches literal $
\|Pipe
\| matches literal |
\\Back slash
\\\\ matches \
\/Forward slash
\/literal\/
\(Opening bracket
\\(
\)Closing bracket
\\)
\[Opening square bracket
\\[
\]Closing square bracket
\\]
\{Opening curly bracket
\\{
\}Closing curly bracket
\\}
\\Back slash (inside character set)
\]Closing square bracket (inside character set)
^Must be escaped only if immediately after [
e.g. [^abc] negates, [abc^] is literal
-Must be escaped if between two letters/digits
e.g. [a-z] is range, [a\-z] is literal -, a, z
{n}Exactly n
\d{3} matches 123
{n,}n or more
\d{2,} matches 12, 123
{n,m}Between n and m
\d{2,4} matches 12-1234
*0 or more
ab*c matches ac, abc, abbc
+1 or more
ab+c matches abc, abbc
?Exactly 0 or 1
colou?r matches color/colour
*?Lazy *
<.*?> matches <a> not <a>b</a>
+?Lazy +
\w+? matches minimal word
^Start of string
^hello matches hello...
$End of string
...world$ matches ...world
\bWord boundary
\bword\b matches word
Matches at start/end of string if char is \w, or between \w and \W
\BNon-word boundary
\Bword\B
foo|barMatch either foo or bar
foo(?=bar)Match foo if it's before bar
foo(?!bar)Match foo if it's not before bar
(?<=bar)fooMatch foo if it's after bar
(?<!bar)fooMatch foo if it's not after bar
(abc)Capturing group; match and capture abc
(?:abc)Non-capturing group; match abc without capturing
\1Backreference to 1st capturing group
(\w)\1 matches aa, bb
(?<name>...)Named capturing group
(?<year>\d{4})
\k<name>Named backreference
\k<year> references year
gGlobal (all matches)
/abc/g finds all abc
iCase-insensitive
/abc/i matches AbC
mMultiline (^/$ per line)
/^test/m
sDotall (. matches newline)
/.+/s
uUnicode
/\p{L}/u
ySticky (lastIndex)
/abc/y