Regex Cheat Sheet

Comprehensive reference guide with examples and syntax

Regex Pro
Professional Tools
Anchors
^
Matches the start of a string (or line in multiline mode)
^Hello
Hello World
Goodbye World
$
Matches the end of a string (or line in multiline mode)
World$
Hello World
Hello Universe
\b
Word boundary - matches between word and non-word characters
\bcat\b
The cat is sleeping
category
\B
Non-word boundary - matches anywhere except word boundaries
\Bcat\B
The cat is sleeping
category
Character Classes
[abc]
Matches any single character: a, b, or c
[aeiou]
The quick brown fox
[^abc]
Matches any character except a, b, or c
[^aeiou]
The quick brown fox
[a-z]
Matches any lowercase letter from a to z
[a-z]+
hello world
\d
Matches any digit (equivalent to [0-9])
\d+
Phone: 123-456-7890
\w
Matches any word character (letter, digit, or underscore)
\w+
Hello_world 123
\s
Matches any whitespace character (space, tab, newline)
\s+
Hello World\nNew Line
Quantifiers
*
Matches 0 or more of the preceding element (greedy)
a*
bbb aaa ccc
+
Matches 1 or more of the preceding element (greedy)
a+
bbb aaa ccc
?
Matches 0 or 1 of the preceding element (greedy)
colou?r
color colour
{n}
Matches exactly n occurrences of the preceding element
\d{3}
123-456-7890
{n,}
Matches n or more occurrences of the preceding element
\d{2,}
123-456
{n,m}
Matches between n and m occurrences of the preceding element
\d{2,4}
12 123 1234 12345
Groups
(abc)
Capturing group - captures the matched text for backreferences
(abc)\1
abcabc
(?:abc)
Non-capturing group - groups without capturing
(?:abc)*
abcabcabc
\1
Backreference to the first capturing group
(abc)\1
abcabc
(?#comment)
Inline comment - ignored by regex engine
a(?#letter a)b
ab
Alternation
a|b|c
Matches a, or b, or c. Evaluated left to right.
cat|dog|bird
The cat and the dog and the bird
Escaped Characters
\. \* \+
Escape special characters to match them literally
\*\*bold\*\*
**bold** text
\D \W \S
Negated character classes (non-digit, non-word, non-space)
\D+
Phone: 123-abc-7890
Lookaround
(?=abc)
Positive lookahead - matches if followed by abc
\w+(?=ing)
running swimming
(?!abc)
Negative lookahead - matches if not followed by abc
\w+(?!ing)
running swimming
(?<=abc)
Positive lookbehind - matches if preceded by abc
(?<=\$)\d+
$123 €456
(?<!abc)
Negative lookbehind - matches if not preceded by abc
(?<!\$)\d+
$123 €456
Flags
/pattern/i
Case insensitive - ignores case when matching
/hello/i
Hello HELLO hello
/pattern/g
Global - finds all matches, not just the first
/\d+/g
123 456 789
/pattern/m
Multiline - ^ and $ match line boundaries
/^test/m
line1
test line2
line3 test
/pattern/s
Dot all - . matches newline characters
/.*/s
line1\nline2
Common Patterns
^\w+@\w+\.\w+$
Simple email validation
user@domain.com
user@domain.com
^\d{4}-\d{2}-\d{2}$
Date format YYYY-MM-DD
2024-12-30
2024-12-30
^\d{3}-\d{3}-\d{4}$
US phone number format
555-123-4567
555-123-4567
^https?://[^\s/$.?#].[^\s]*$
Basic URL validation
https://example.com
https://example.com