DevBolt

Regex Cheat Sheet

Interactive regular expression reference with 60 patterns. Search, filter by category, copy any pattern, or click “Try it” to test in the Regex Tester.

Showing 60 of 60 patterns

Metacharacters

.
Matches any character except newline
a.c matches "abc", "a1c", "a-c"
Try it
^
Start of string (or line in multiline mode)
^Hello matches "Hello world"
Try it
$
End of string (or line in multiline mode)
world$ matches "Hello world"
Try it
\
Escapes a metacharacter to match it literally
\. matches a literal dot "."
Try it
|
Alternation — matches the expression before or after
cat|dog matches "cat" or "dog"
Try it
()
Grouping — groups expressions and captures matches
(ab)+ matches "abab"
Try it
[]
Character class — matches any single character inside
[aeiou] matches any vowel
Try it

Character Classes

[abc]
Matches any one of a, b, or c
[abc] in "apple" matches "a"
Try it
[^abc]
Matches any character NOT a, b, or c
[^abc] in "apple" matches "p"
Try it
[a-z]
Matches any lowercase letter a through z
[a-z]+ matches "hello"
Try it
[A-Z0-9]
Matches any uppercase letter or digit
[A-Z0-9] matches "A" or "5"
Try it
\d
Matches any digit (equivalent to [0-9])
\d{3} matches "123"
Try it
\D
Matches any non-digit character
\D+ matches "abc"
Try it
\w
Matches any word character [a-zA-Z0-9_]
\w+ matches "hello_42"
Try it
\W
Matches any non-word character
\W matches "@" in "a@b"
Try it
\s
Matches any whitespace (space, tab, newline)
\s+ matches " " (spaces)
Try it
\S
Matches any non-whitespace character
\S+ matches "hello"
Try it
\t
Matches a tab character
\t matches tab in "a\tb"
Try it
\n
Matches a newline character
\n matches line breaks
Try it

Quantifiers

*
Matches 0 or more of the preceding element (greedy)
ab*c matches "ac", "abc", "abbc"
Try it
+
Matches 1 or more of the preceding element (greedy)
ab+c matches "abc", "abbc" but not "ac"
Try it
?
Matches 0 or 1 of the preceding element
colou?r matches "color" and "colour"
Try it
{n}
Matches exactly n occurrences
\d{4} matches "2026"
Try it
{n,}
Matches n or more occurrences
\d{2,} matches "42" and "123"
Try it
{n,m}
Matches between n and m occurrences
\d{2,4} matches "42", "123", "2026"
Try it
*? / +?
Lazy quantifiers -- match as few characters as possible
<.*?> matches "<b>" in "<b>bold</b>"
Try it
*+ / ++
Possessive quantifiers -- no backtracking (if supported)
a++b prevents backtracking on a's
Try it

Anchors & Boundaries

^
Matches the start of the string
^foo matches "foo bar" but not "bar foo"
Try it
$
Matches the end of the string
bar$ matches "foo bar" but not "bar foo"
Try it
\b
Word boundary -- between a word and non-word character
\bcat\b matches "cat" not "catch"
Try it
\B
Non-word boundary -- NOT at a word boundary
\Bcat matches "scat" but not "cat"
Try it
\A / \Z
Absolute start / end of string (ignores multiline flag)
\Afoo matches only at very start of input
Try it

Groups & Lookaround

(abc)
Capturing group -- captures matched text for backreference
(\d+)-(\d+) captures "12" and "34"
Try it
(?:abc)
Non-capturing group -- groups without capturing
(?:ab)+ matches "abab" without capture
Try it
(?<name>abc)
Named capturing group
(?<year>\d{4}) captures and names it "year"
Try it
\1
Backreference -- matches same text as group 1
(\w+)\s\1 matches "the the"
Try it
(?=abc)
Positive lookahead -- asserts what follows matches
\d(?=px) matches "5" in "5px"
Try it
(?!abc)
Negative lookahead -- asserts what follows does NOT match
\d(?!px) matches "5" in "5em"
Try it
(?<=abc)
Positive lookbehind -- asserts what precedes matches
(?<=\$)\d+ matches "50" in "$50"
Try it
(?<!abc)
Negative lookbehind -- asserts what precedes does NOT match
(?<!\$)\d+ matches "50" in "50 items"
Try it

Flags

g
Global -- find all matches, not just the first
/a/g in "banana" matches all 3 a's
Try it
i
Case-insensitive matching
/hello/i matches "Hello", "HELLO"
Try it
m
Multiline -- ^ and $ match line starts/ends
/^foo/m matches "foo" on any line
Try it
s
Dotall -- makes . match newline characters too
/a.b/s matches "a\nb"
Try it
u
Unicode -- enables full Unicode matching
/\u{1F600}/u matches emoji
Try it
y
Sticky -- matches only at lastIndex position
/foo/y matches at exact position
Try it

Common Patterns

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Email address
user@example.com
Try it
https?:\/\/[\w\-._~:/?#\[\]@!$&'()*+,;=%]+
URL (http/https)
https://devbolt.dev/tools
Try it
\b\d{1,3}(\.\d{1,3}){3}\b
IPv4 address
192.168.1.1
Try it
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Date in YYYY-MM-DD format
2026-03-19
Try it
#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})
Hex color code
#1a2b3c or #abc
Try it
\+?\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}
Phone number (international)
+1 (555) 123-4567
Try it
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
UUID v4 format
550e8400-e29b-41d4-a716-446655440000
Try it
(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}
Strong password (8+ chars, upper, lower, digit, special)
P@ssw0rd!
Try it
<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)<\/\1>
HTML tag with content (simple)
<b>bold text</b>
Try it
^\s+|\s+$
Leading and trailing whitespace (for trimming)
" hello " captures spaces
Try it
\d{1,3}(,\d{3})*(\.\d+)?
Number with commas and optional decimals
1,234,567.89
Try it
[a-z0-9]+(?:-[a-z0-9]+)*
URL-friendly slug
my-blog-post-title
Try it
\b[A-Z][a-z]+(?:\s[A-Z][a-z]+)*\b
Capitalized words (proper names)
John Smith
Try it
(?:25[0-5]|2[0-4]\d|[01]?\d\d?)
Single IPv4 octet (0-255)
255, 192, 0
Try it

Frequently Asked Questions

What are the most common regex metacharacters?
The most common regex metacharacters are: . (matches any character except newline), ^ (start of string), $ (end of string), * (zero or more), + (one or more), ? (zero or one), \ (escape character), | (alternation/OR), () (grouping and capturing), and [] (character class). These form the foundation of all regular expression patterns.
How do lookaheads and lookbehinds work in regex?
Lookaheads and lookbehinds are zero-width assertions that match a position without consuming characters. A positive lookahead (?=abc) matches if 'abc' follows the current position. A negative lookahead (?!abc) matches if 'abc' does NOT follow. A positive lookbehind (?<=abc) matches if 'abc' precedes the current position. A negative lookbehind (?<!abc) matches if 'abc' does NOT precede. They are powerful for matching patterns that depend on surrounding context without including that context in the match.
What's the difference between greedy and lazy quantifiers in regex?
Greedy quantifiers (*, +, {n,m}) match as many characters as possible while still allowing the overall pattern to match. Lazy quantifiers (*?, +?, {n,m}?) match as few characters as possible. For example, given the string '<b>bold</b>', the greedy pattern '<.*>' matches the entire string '<b>bold</b>', while the lazy pattern '<.*?>' matches just '<b>'. Use lazy quantifiers when you want the shortest possible match.
How do I test and debug regex patterns?
You can test regex patterns using DevBolt's free Regex Tester tool at devbolt.dev/tools/regex-tester. It provides real-time match highlighting, capture group inspection, and match details as you type. For generating patterns from plain English descriptions, try the Regex Generator at devbolt.dev/tools/regex-generator. Both tools run entirely in your browser with no signup required.