← Home

Regex Cheatsheet

Quick reference for regular expression syntax — character classes, quantifiers, anchors, groups, lookaheads, and common real-world patterns.

Character Classes

.
Any character except newlinea.c → abc, a1c, a-c
\d
Digit (0-9)\d{3} → 123, 456
\D
Not a digit
\w
Word character (a-z, A-Z, 0-9, _)\w+ → hello_world
\W
Not a word character
\s
Whitespace (space, tab, newline)\s+ → matches spaces
\S
Not whitespace
[abc]
Any of a, b, or c[aeiou] → matches vowels
[^abc]
Not a, b, or c
[a-z]
Any character in range a-z
[A-Z0-9]
Any uppercase letter or digit

Anchors

^
Start of string / line^Hello → matches 'Hello world'
$
End of string / lineworld$ → matches 'Hello world'
\b
Word boundary\bcat\b → 'cat' but not 'scatter'
\B
Not a word boundary

Quantifiers

*
0 or moreab*c → ac, abc, abbc
+
1 or moreab+c → abc, abbc (not ac)
?
0 or 1 (optional)colou?r → color, colour
{n}
Exactly n times\d{4} → 2024
{n,}
n or more times
{n,m}
Between n and m times\d{2,4} → 12, 123, 1234
*?
0 or more (lazy / non-greedy)
+?
1 or more (lazy / non-greedy)

Groups & References

(abc)
Capturing group(\d+)-(\d+) → captures both numbers
(?:abc)
Non-capturing group
(?<name>abc)
Named capturing group
\1
Back-reference to group 1(\w)\1 → matches 'aa', 'bb'
(a|b)
Alternation (a or b)(cat|dog) → cat or dog

Lookahead & Lookbehind

(?=abc)
Positive lookahead\d(?=px) → 5 in '5px'
(?!abc)
Negative lookahead\d(?!px) → 5 in '5em'
(?<=abc)
Positive lookbehind(?<=\$)\d+ → 100 in '$100'
(?<!abc)
Negative lookbehind

Flags

g
Global — match all occurrences, not just the first
i
Case-insensitive matching
m
Multiline — ^ and $ match line boundaries
s
Dotall — . matches newline characters too
u
Unicode — treat pattern as Unicode code points

Common Patterns

^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$
Email address (basic)
^https?://[^\s]+$
URL (basic)
^\d{1,3}(\.\d{1,3}){3}$
IPv4 address
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
HEX color code
^\d{4}-\d{2}-\d{2}$
Date (YYYY-MM-DD)
^\+?[\d\s-]{7,15}$
Phone number (international)
^(?=.*[A-Z])(?=.*\d).{8,}$
Password (min 8 chars, 1 uppercase, 1 digit)

FAQ

What is the difference between * and + in regex?

* matches 0 or more occurrences (the preceding element is optional), while + matches 1 or more (at least one occurrence is required). For example, ab*c matches 'ac' and 'abc', but ab+c only matches 'abc' (not 'ac').

What is a non-greedy (lazy) quantifier?

By default, quantifiers like * and + are greedy — they match as much as possible. Adding ? makes them lazy, matching as little as possible. For example, on '<b>bold</b>', <.*> matches the entire string (greedy), while <.*?> matches just '<b>' (lazy).

How do I match a literal dot or bracket in regex?

Escape special characters with a backslash: \. matches a literal dot, \[ matches a literal bracket, and \\ matches a literal backslash. Inside a character class [.], the dot is already literal.

What is a lookahead in regex?

A lookahead checks if a pattern exists ahead of the current position without consuming characters. (?=abc) is a positive lookahead (succeeds if abc follows), and (?!abc) is a negative lookahead (succeeds if abc does NOT follow). They're useful for validating conditions like passwords.

Related Resources