9kit.org

Regex Cheatsheet

A comprehensive regex quick reference guide organized by category. Click any pattern to copy it to your clipboard. Perfect for developers who need to look up regex syntax quickly.

12
Character Classes

.

Any character except newline

Example:c.tcat, cut, cbt
\d

Digit (0-9)

Example:\d+123, 42, 0
\D

Non-digit

Example:\D+abc, xyz
\w

Word character (a-z, A-Z, 0-9, _)

Example:\w+hello_world
\W

Non-word character

Example:\W!, @, #
\s

Whitespace (space, tab, newline)

Example:\s+ , \t, \n
\S

Non-whitespace

Example:\S+hello
[abc]

Any of a, b, or c

Example:[aeiou]a, e, i, o, u
[^abc]

Not a, b, or c

Example:[^aeiou]b, c, d
[a-z]

Character range a to z

Example:[a-z]+hello
[A-Z]

Character range A to Z

Example:[A-Z]+HELLO
[0-9]

Digit range 0 to 9

Example:[0-9]+12345

8
Anchors

^

Start of string/line

Example:^HelloHello at start
$

End of string/line

Example:world$world at end
\b

Word boundary

Example:\bword\bwhole word only
\B

Non-word boundary

Example:\Bwordpassword
(?=...)

Positive lookahead

Example:\d+(?=px)100 in "100px"
(?!...)

Negative lookahead

Example:\d+(?!px)100 in "100em"
(?<=...)

Positive lookbehind

Example:(?<=\$)\d+100 in "$100"
(?<!...)

Negative lookbehind

Example:(?<!\$)\d+100 in "100"

9
Quantifiers

*

Zero or more

Example:ab*cac, abc, abbc
+

One or more

Example:ab+cabc, abbc
?

Zero or one (optional)

Example:colou?rcolor, colour
{n}

Exactly n times

Example:\d{3}123
{n,}

n or more times

Example:\d{2,}12, 123, 1234
{n,m}

Between n and m times

Example:\d{2,4}12, 123, 1234
*?

Zero or more (lazy)

Example:a.*?bshortest match
+?

One or more (lazy)

Example:a.+?bshortest match
??

Zero or one (lazy)

Example:a??bshortest match

6
Groups & References

(...)

Capturing group

Example:(\d+)pxGroup: 100
(?:...)

Non-capturing group

Example:(?:ab)+ababab
(?<name>...)

Named capturing group

Example:(?<num>\d+)Group "num"
\1

Backreference to group 1

Example:(\w)\1aa, bb, cc
\k<name>

Named backreference

Example:\k<word>References named group
(a|b)

Alternation (or)

Example:(cat|dog)cat or dog

5
Flags

g

Global - find all matches

Example:/\d/gAll digits
i

Case insensitive

Example:/hello/iHELLO, Hello
m

Multiline mode

Example:/^line/mLine starts
s

Dotall - . matches newline

Example:/a.b/sa\nb
u

Unicode support

Example:/\u{1F600}/uEmoji

8
Special Characters

\n

Newline

Example:line1\nline2Two lines
\r

Carriage return

Example:a\rbab
\t

Tab

Example:col1\tcol2Two columns
\0

Null character

Example:a\0ba\0b
\\

Escaped backslash

Example:\\nLiteral \n
\.

Escaped dot (literal)

Example:3\.143.14
\^

Escaped caret (literal)

Example:\^start^start
\$

Escaped dollar (literal)

Example:end\$end$

Frequently Asked Questions

Tags

regex
regular expression
cheatsheet
reference
pattern