Last updated
Regex Cheat Sheet Examples
The Regex Cheat Sheet is a comprehensive interactive reference for regular expression syntax. Below are the key syntax elements with examples organized by category.
Character Classes
Shortcuts for common character sets:
. Any character except newline
\d Any digit [0-9]
\D Any non-digit
\w Any word character [a-zA-Z0-9_]
\W Any non-word character
\s Any whitespace (space, tab, newline)
\S Any non-whitespace
Custom classes:
[abc] Matches a, b, or c
[a-z] Any lowercase letter
[A-Z] Any uppercase letter
[0-9] Any digit (same as \d)
[^abc] Any character NOT a, b, or c
[a-zA-Z0-9] Any alphanumeric character
Quantifiers
Control how many times a pattern repeats:
* Zero or more (greedy)
+ One or more (greedy)
? Zero or one (optional)
{3} Exactly 3 times
{3,} 3 or more times
{3,6} Between 3 and 6 times
Lazy (match as few as possible):
*? Zero or more (lazy)
+? One or more (lazy)
?? Zero or one (lazy)
{3,6}? Between 3 and 6 (lazy)
Example:
Input: <b>text</b>
/<.+>/ matches <b>text</b> (greedy — whole string)
/<.+?>/ matches <b> (lazy — shortest match)
Anchors and Boundaries
Control where in the string a match can occur:
^ Start of string (or line with /m flag)
$ End of string (or line with /m flag)
\b Word boundary
\B Non-word boundary
\A Absolute start of string (Python, Java)
\Z Absolute end of string (Python, Java)
Examples:
/^hello/ matches "hello world" but not "say hello"
/world$/ matches "hello world" but not "world peace"
/\bcat\b/ matches "cat" but not "catch" or "tomcat"
Groups and References
Capture and reuse matched text:
(abc) Capturing group — saves match for backreference
(?:abc) Non-capturing group — groups without saving
(?<name>abc) Named capturing group
\1 Backreference to group 1
\k<name> Backreference to named group
Example — match repeated words:
/\b(\w+)\s+\1\b/
Matches: "the the", "is is", "hello hello"
Named group example:
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
Input: "2026-03-17"
Groups: year=2026, month=03, day=17
Lookahead and Lookbehind
Match based on context without including it in the result:
(?=...) Positive lookahead — followed by
(?!...) Negative lookahead — NOT followed by
(?<=...) Positive lookbehind — preceded by
(?<!...) Negative lookbehind — NOT preceded by
Examples:
/\d+(?= dollars)/ matches "100" in "100 dollars"
/\d+(?! dollars)/ matches "100" in "100 euros"
/(?<=\$)\d+/ matches "50" in "$50"
/(?<!\$)\d+/ matches "50" in "50 items" but not "$50"
Alternation
Match one of several alternatives:
cat|dog Matches "cat" or "dog"
(cat|dog)s Matches "cats" or "dogs"
Example:
/(jpg|jpeg|png|gif|webp)$/i
Matches any common image file extension
Regex Flags
Modifiers that change matching behavior:
Flag Name Effect
i Case-insensitive /hello/i matches "Hello", "HELLO"
g Global Find all matches, not just first
m Multiline ^ and $ match start/end of each line
s Dotall . matches newlines too
u Unicode Enable full Unicode support
x Extended (PCRE) Allow whitespace and comments in pattern
JavaScript: /pattern/gim
Python: re.compile(r'pattern', re.IGNORECASE | re.MULTILINE)
Java: Pattern.compile("pattern", Pattern.CASE_INSENSITIVE)
Common Validation Patterns
Ready-to-use patterns for frequent validation tasks:
Email:
/^[\w.\-]+@[\w\-]+\.[a-zA-Z]{2,6}$/
URL:
/^https?:\/\/[\w\-]+(\.[\w\-]+)+(\/[\w\-._~:/?#[\]@!$&'()*+,;=%]*)?$/
IPv4 address:
/^(\d{1,3}\.){3}\d{1,3}$/
US phone:
/^\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$/
Date YYYY-MM-DD:
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
Hex color:
/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
Postal code (US ZIP):
/^\d{5}(-\d{4})?$/
Strong password (8+ chars, upper, lower, digit, special):
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
Language-Specific Differences
Key syntax differences between regex flavors:
Named groups:
JavaScript: (?<name>...)
Python: (?P<name>...)
Java: (?<name>...)
PCRE: (?<name>...)
Backreference to named group:
JavaScript: \k<name>
Python: (?P=name)
Java: \k<name>
String escaping in Java (double backslash required):
Java string: "\\d{4}-\\d{2}-\\d{2}"
Regex: \d{4}-\d{2}-\d{2}
Multiline flag:
JavaScript: /pattern/m
Python: re.MULTILINE
Java: Pattern.MULTILINE
Escape Special Characters
Characters that must be escaped with a backslash:
. * + ? ^ $ { } [ ] | ( ) \
To match a literal dot: \.
To match a literal star: \*
To match a literal paren: \(
Example — match a price like $9.99:
/\$\d+\.\d{2}/
- Character class shortcuts: \d, \w, \s and their negations
- Greedy and lazy quantifiers with clear examples
- Anchors for start, end, and word boundaries
- Capturing, non-capturing, and named groups
- Lookahead and lookbehind assertions
- Regex flags for case, multiline, global, and dotall modes
- Ready-to-use patterns for email, URL, phone, date, and more
- Language-specific syntax differences for JS, Python, Java, PCRE