$ tpt --version 1.0
tpt
About
online
01 Jan 1970
00:00:00
Text Utilities
/regex-cheatsheet

Regex Cheatsheet

Quick reference for regular expression syntax and patterns

all tools
Normal characters
.

Any character excluding a newline or carriage return

[A-Za-z]

Alphabet

[a-z]

Lowercase alphabet

[A-Z]

Uppercase alphabet

\d

Digit [0-9]

\D

Non-digit [^0-9]

_

Underscore

\w

Alphabet, digit or underscore [A-Za-z0-9_]

\W

Inverse of \w [^A-Za-z0-9_]

\S

Inverse of \s

Whitespace characters

Space

\t

Tab

\n

Newline

\r

Carriage return

\s

Space, tab, newline or carriage return

Character set
[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

Characters that require escaping
\.

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

\\}

Character set escaping rules
\\

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

Quantifiers
{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

Anchors
^

Start of string

^hello matches hello...

$

End of string

...world$ matches ...world

\b

Word boundary

\bword\b matches word

Matches at start/end of string if char is \w, or between \w and \W

\B

Non-word boundary

\Bword\B

Alternation & lookaround
foo|bar

Match either foo or bar

foo(?=bar)

Match foo if it's before bar

foo(?!bar)

Match foo if it's not before bar

(?<=bar)foo

Match foo if it's after bar

(?<!bar)foo

Match foo if it's not after bar

Groups
(abc)

Capturing group; match and capture abc

(?:abc)

Non-capturing group; match abc without capturing

\1

Backreference to 1st capturing group

(\w)\1 matches aa, bb

(?<name>...)

Named capturing group

(?<year>\d{4})

\k<name>

Named backreference

\k<year> references year

Flags
g

Global (all matches)

/abc/g finds all abc

i

Case-insensitive

/abc/i matches AbC

m

Multiline (^/$ per line)

/^test/m

s

Dotall (. matches newline)

/.+/s

u

Unicode

/\p{L}/u

y

Sticky (lastIndex)

/abc/y