Regex Cheat Sheet

Comprehensive regular expression quick reference with 42 patterns across 7 categories. Search and click to copy.

Character Classes

Pattern
Description
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 character (a-z, A-Z, 0-9, _)
\w+ matches "hello_123"
\W
Non-word character
\W matches "@", " "
\s
Whitespace (space, tab, newline)
a\sb matches "a b"
\S
Non-whitespace
\S+ matches "hello"
[abc]
Any of a, b, or c
[aeiou] matches vowels
[^abc]
Not a, b, or c
[^0-9] matches non-digits
[a-z]
Range: a to z
[a-zA-Z] matches any letter

Anchors

Pattern
Description
Example
^
Start of string/line
^Hello matches "Hello world"
$
End of string/line
world$ matches "Hello world"
\b
Word boundary
\bcat\b matches "cat" not "catch"
\B
Non-word boundary
\Bcat\B matches "concatenate"

Quantifiers

Pattern
Description
Example
*
Zero or more
ab*c matches "ac", "abc", "abbc"
+
One or more
ab+c matches "abc", "abbc" not "ac"
?
Zero or one (optional)
colou?r matches "color", "colour"
{n}
Exactly n times
\d{4} matches "2024"
{n,}
n or more times
\d{2,} matches "12", "123"
{n,m}
Between n and m times
\d{2,4} matches "12", "1234"
*?
Zero or more (lazy)
<.*?> matches first tag only
+?
One or more (lazy)
.+? matches as few as possible

Groups & References

Pattern
Description
Example
(abc)
Capturing group
(\d+)-(\d+) captures both parts
(?:abc)
Non-capturing group
(?:ab)+ matches "abab"
(?<name>abc)
Named capturing group
(?<year>\d{4}) captures as "year"
\1
Back-reference to group 1
(\w+)\s\1 matches "the the"
(a|b)
Alternation (a or b)
(cat|dog) matches either

Lookaround

Pattern
Description
Example
(?=abc)
Positive lookahead
\d(?=px) matches "5" in "5px"
(?!abc)
Negative lookahead
\d(?!px) matches "5" in "5em"
(?<=abc)
Positive lookbehind
(?<=\$)\d+ matches "100" in "$100"
(?<!abc)
Negative lookbehind
(?<!\$)\d+ matches "100" in "100"

Flags

Pattern
Description
Example
g
Global - match all occurrences
/cat/g matches all "cat" instances
i
Case-insensitive matching
/hello/i matches "Hello", "HELLO"
m
Multiline - ^ and $ match line start/end
/^line/m matches each line start
s
Dotall - . matches newline too
/a.b/s matches "a\nb"
u
Unicode support
/\p{L}/u matches Unicode letters

Common Patterns

Pattern
Description
Example
^[\w.-]+@[\w.-]+\.\w{2,}$
Email (basic)
user@example.com
^https?://[\w.-]+(?:/[\w.-]*)*$
URL (basic)
https://example.com/path
^\d{1,3}(\.\d{1,3}){3}$
IPv4 address
192.168.1.1
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Hex color
#ff6600 or #f60
^\d{4}-\d{2}-\d{2}$
Date (YYYY-MM-DD)
2024-01-15
^\+?\d{1,3}[-.\s]?\d{4,14}$
Phone number (basic)
+1-555-1234567

How to Use the Regex Cheat Sheet

  1. 1.Browse categories or use the search bar to find specific patterns, descriptions, or examples.
  2. 2.Click a category button to filter by topic (Character Classes, Quantifiers, Lookaround, etc.).
  3. 3.Click on any row to copy the pattern to your clipboard.
  4. 4.Use the Common Patterns section for ready-to-use regex for emails, URLs, dates, and more.

Frequently Asked Questions

What is a regular expression (regex)?

A regular expression is a sequence of characters that defines a search pattern. It is used for pattern matching in strings -- finding, replacing, or validating text. Regex is supported in virtually all programming languages and text editors.

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers (*, +, {n,m}) match as much text as possible. Lazy quantifiers (*?, +?, {n,m}?) match as little as possible. For example, given "<b>bold</b>", the pattern <.*> (greedy) matches the entire string, while <.*?> (lazy) matches just "<b>".

What are lookahead and lookbehind?

Lookahead (?=...) and lookbehind (?<=...) are zero-width assertions. They check for a pattern without including it in the match. Positive variants assert the pattern exists; negative variants (?!...) and (?<!...) assert it does not exist.

How do I test my regex patterns?

You can use our Regex Tester tool at /regex-tester to test patterns with real-time matching and highlighting. Online tools like regex101.com also provide detailed explanations of each pattern component.