Regex Cheatsheet

Quick reference for regular expression syntax. Search or browse by category and click any token to copy.

Character Classes(10)
.

Any character

Matches any single character except newline

Example

a.c matches abc
\d

Digit

Matches any digit (0-9)

Example

\d\d matches 42
\w

Word character

Matches letter, digit, or underscore

Example

\w+ matches hello_123
\s

Whitespace

Matches space, tab, newline

Example

a\sb matches "a b"
\D

Non-digit

Opposite of \d

Example

\D matches a
\W

Non-word

Opposite of \w

Example

\W matches !
\S

Non-whitespace

Opposite of \s

Example

\S matches a
[abc]

Character set

Matches any of a, b, c

Example

[aeiou] matches e
[^abc]

Negated set

Matches any character NOT a, b, c

Example

[^aeiou] matches s
[a-z]

Range

Matches characters a through z

Example

[a-z]+ matches hello
Anchors(4)
^

Start of string

Anchor that matches at the start of the string

Example

^Hello matches at start
$

End of string

Anchor that matches at the end of the string

Example

world$ matches at end
\b

Word boundary

Anchor that matches at a word boundary

Example

\bword\b matches whole word
\B

Non-word boundary

Anchor that matches where there is NOT a word boundary

Example

\Bend matches "end" within "weekend"
Quantifiers(8)
*

Zero or more

Matches the preceding token zero or more times

Example

ab*c matches ac, abc, abbc
+

One or more

Matches the preceding token one or more times

Example

ab+c matches abc, abbc
?

Zero or one

Matches the preceding token zero or one time (optional)

Example

colou?r matches color, colour
{n}

Exactly n

Matches the preceding token exactly n times

Example

a{3} matches aaa
{n,}

At least n

Matches the preceding token n or more times

Example

a{2,} matches aa, aaa
{n,m}

Between n and m

Matches the preceding token between n and m times

Example

a{2,4} matches aa to aaaa
*?

Lazy zero or more

Matches as few as possible of the preceding token

Example

a.*?b matches "axxb" stops at first b
+?

Lazy one or more

Matches as few as possible (at least one)

Example

a.+?b matches "axxb" stops at first b
Groups & Lookarounds(6)
(abc)

Capturing group

Groups a pattern and captures the matched text

Example

(ab)+ matches abab
(?:abc)

Non-capturing group

Groups a pattern without capturing the matched text

Example

(?:ab)+ matches abab
(?=abc)

Positive lookahead

Asserts that what follows matches without consuming

Example

a(?=b) matches a only if followed by b
(?!abc)

Negative lookahead

Asserts that what follows does NOT match

Example

a(?!b) matches a only if NOT followed by b
(?<=abc)

Positive lookbehind

Asserts that what precedes matches without consuming

Example

(?<=a)b matches b only if preceded by a
(?<!abc)

Negative lookbehind

Asserts that what precedes does NOT match

Example

(?<!a)b matches b only if NOT preceded by a
Alternation(1)
|

Or

Acts as an OR operator between alternatives

Example

cat|dog matches cat or dog
Escaped Characters(7)
\n

Newline

Matches a line feed character

Example

a\nb matches "a" then new line "b"
\r

Carriage return

Matches a carriage return character

Example

\r\n matches CRLF line ending
\t

Tab

Matches a horizontal tab character

Example

a\tb matches "a<TAB>b"
\\

Literal backslash

Matches a literal backslash character

Example

a\\b matches a\b
\.

Literal dot

Matches a literal period/dot character

Example

example\.com matches example.com
\xHH

Hex character

Matches the character with the specified hex code

Example

\x41 matches A
\uHHHH

Unicode character

Matches the Unicode character with the specified hex code

Example

\u00A9 matches ©