Regex Cheatsheet

Character Classes

Pattern Matches Example

.

Any character except newline

a.c matches "abc", "a1c"

\d

Digit [0-9]

\d{3} matches "123"

\D

Non-digit

\D+ matches "abc"

\w

Word char [a-zA-Z0-9_]

\w+ matches "var_1"

\W

Non-word char

\W matches "@", " "

\s

Whitespace [ \t\n\r\f]

\s+ matches spaces/tabs

\S

Non-whitespace

\S+ matches "word"

[abc]

Character set — a, b, or c

[aeiou] matches vowels

[^abc]

Negated set — not a, b, or c

[^0-9] matches non-digits

[a-z]

Range

[A-Fa-f0-9] matches hex chars

Anchors

Pattern Matches Example

^

Start of line

^Error matches "Error: …​"

$

End of line

\.conf$ matches "sshd.conf"

\b

Word boundary

\bcat\b matches "cat" not "concatenate"

\B

Non-word boundary

\Bcat matches "concatenate"

Quantifiers

Pattern Matches Notes

*

0 or more (greedy)

a* matches "", "a", "aaa"

+

1 or more (greedy)

a+ matches "a", "aaa" not ""

?

0 or 1 (optional)

colou?r matches "color", "colour"

{n}

Exactly n

\d{4} matches "2026"

{n,}

n or more

\d{2,} matches "12", "123"

{n,m}

Between n and m

\d{1,3} matches "1", "12", "123"

*?

0 or more (lazy)

Matches as few as possible

+?

1 or more (lazy)

Matches as few as possible

Groups and Alternation

Pattern Purpose Example

(abc)

Capture group

(error|warn) captures match

(?:abc)

Non-capturing group

(?:http|https):// groups without capturing

\1

Backreference to group 1

(\w+)\s+\1 matches "the the"

a|b

Alternation (or)

cat|dog matches "cat" or "dog"

Lookaround (PCRE)

Pattern Purpose Example

(?=…​)

Lookahead (followed by)

\d+(?=px) matches "12" in "12px"

(?!…​)

Negative lookahead

\d+(?!px) matches "12" in "12em"

(?⇐…​)

Lookbehind (preceded by)

(?⇐\$)\d+ matches "50" in "$50"

(?<!…​)

Negative lookbehind

(?<!\$)\d+ matches "50" in "EUR50"

Common Patterns

IP address (basic)
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Email (simple)
[\w.+-]+@[\w-]+\.[\w.]+
MAC address
([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}
Log timestamp (ISO 8601)
\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}

CLI Usage

grep with PCRE
grep -P '(?<=error:\s)\w+' /var/log/syslog
sed capture groups
echo "2026-04-10" | sed 's/\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\2\/\3\/\1/'
awk regex match
awk '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {print $1}' access.log