Regex Cheatsheet

This cheatsheet provides a comprehensive and practical reference for regular expressions. It covers character classes, anchors, quantifiers, groups, common patterns, command combos, and more. Use it to boost your productivity in pattern matching and text processing.

Regex Cheatsheet

Regular expression patterns and syntax

60

Patterns

7

Categories

0

Favorites

16

Sections

Characters
Basic Classes
.

Any character except newline

e.g., a.c → abc

\d

Digit (0-9)

e.g., \d\d → 42

\D

Non-digit

\w

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

\W

Non-word character

\s

Whitespace

\S

Non-whitespace

Characters
Character Sets
[abc]

Match a, b, or c

[^abc]

Match any except a, b, c

[a-z]

Lowercase letter range

[A-Z]

Uppercase letter range

[0-9]

Digit range

[a-zA-Z0-9]

Alphanumeric

Characters
Escape Characters
\\

Backslash

\.

Literal dot

\*

Literal asterisk

\+

Literal plus

\?

Literal question mark

💡 Escape special regex characters with backslash

Anchors
Position Anchors
^

Start of string/line

e.g., ^Hello

$

End of string/line

e.g., world$

\b

Word boundary

e.g., \bcat\b

\B

Non-word boundary

Anchors
Examples
^abc$

Exact match 'abc'

^\d+$

String of only digits

^[a-z]+$

Only lowercase letters

Quantifiers
Basic
*

0 or more

e.g., ab*c → ac, abc, abbc

+

1 or more

e.g., ab+c → abc, abbc

?

0 or 1 (optional)

e.g., colou?r

{n}

Exactly n times

e.g., \d{4}

{n,}

n or more times

{n,m}

Between n and m times

Quantifiers
Greedy vs Lazy
*?

Lazy 0 or more (shortest)

+?

Lazy 1 or more

??

Lazy 0 or 1

💡 Greedy matches as much as possible; lazy matches as little

Groups
Capturing
(abc)

Capture group

(?:abc)

Non-capturing group

(?<name>abc)

Named capture group

\1

Reference to group 1

Groups
Lookahead
(?=abc)

Positive lookahead

(?!abc)

Negative lookahead

Groups
Lookbehind
(?<=abc)

Positive lookbehind

(?<!abc)

Negative lookbehind

Groups
Alternation
a|b

Match a or b

(a|b)c

Group alternation

Flags
Common Flags
i

Case-insensitive

g

Global (find all)

m

Multiline

s

Dotall (. matches newline)

u

Unicode support

Common
Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Email

^https?://[\w.-]+(?:/[\w./-]*)?$

URL

^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$

Hex color

Common
Date & Numbers
^\d{4}-\d{2}-\d{2}$

Date (YYYY-MM-DD)

^-?\d+$

Integer

^-?\d*\.?\d+$

Any number

Common
Phone
^\d{3}-\d{3}-\d{4}$

US phone (123-456-7890)

^\+?[1-9]\d{1,14}$

E.164 format

Advanced
Unicode
\p{L}

Any letter (Unicode)

\p{N}

Any number (Unicode)

\p{Emoji}

Emoji characters

💡 Requires Unicode flag (/u)

Quick Reference

Any char

.

Digit

\d

Word

\w

Whitespace

\s

Categories

  • Character Classes

    Match specific types or ranges of characters, such as digits, letters, or whitespace.

  • Anchors

    Match positions in the string, such as the start, end, or word boundaries.

  • Quantifiers

    Specify how many times a pattern should occur.

  • Groups & References

    Group patterns, capture matches, and use backreferences or lookarounds.

  • Common Patterns

    Ready-to-use regexes for emails, dates, URLs, numbers, and more.

  • Command Combos

    Powerful multi-step workflows and advanced usage patterns for real-world scenarios.

Features

  • Quick search functionality
  • Organized by categories
  • Clear pattern descriptions
  • Common and advanced use cases covered
  • Easy to copy patterns
  • Responsive design
  • Perfect for quick reference