Regex Cheat Sheet: Quick Reference Guide
The ultimate regex cheat sheet with all the patterns, metacharacters, and examples you need. A quick reference guide for regular expressions.
Welcome to the ultimate regex cheat sheet! This comprehensive reference guide contains all the regular expression patterns, metacharacters, and examples you need to master regex. Bookmark this page for quick access to all the essential regex information.
Basic Patterns
Literal Characters
Match characters literally:
hello # Matches "hello"
Regex # Matches "Regex"
123 # Matches "123"
Character Classes
Match any one character from a set:
[abc] # Matches 'a', 'b', or 'c'
[a-z] # Matches any lowercase letter
[A-Z] # Matches any uppercase letter
[0-9] # Matches any digit
[a-zA-Z0-9] # Matches any alphanumeric character
[^abc] # Matches any character EXCEPT 'a', 'b', or 'c'
[^0-9] # Matches any non-digit character
Shorthand Character Classes
\d # Any digit (same as [0-9])
\D # Any non-digit (same as [^0-9])
\w # Any word character (same as [a-zA-Z0-9_])
\W # Any non-word character
\s # Any whitespace character (space, tab, newline)
\S # Any non-whitespace character
Metacharacters
Wildcard
. # Matches any single character (except newline)
Anchors
^ # Start of string or line
$ # End of string or line
\b # Word boundary
\B # Non-word boundary
Quantifiers
* # Zero or more occurrences
+ # One or more occurrences
? # Zero or one occurrence
{n} # Exactly n occurrences
{n,} # n or more occurrences
{n,m} # Between n and m occurrences
*? # Zero or more (lazy/non-greedy)
+? # One or more (lazy)
?? # Zero or one (lazy)
{n,}? # n or more (lazy)
{n,m}? # Between n and m (lazy)
Groups
(abc) # Capturing group
(?:abc) # Non-capturing group
(a|b) # OR - matches 'a' or 'b'
(?=abc) # Positive lookahead
(?!abc) # Negative lookahead
(?<=abc) # Positive lookbehind
(?<!abc) # Negative lookbehind
(?#comment) # Comment (some engines)
Escape Characters
Special characters that need escaping:
\. # Literal dot (not any character)
\* # Literal asterisk (not zero or more)
\+ # Literal plus (not one or more)
\? # Literal question mark (not zero or one)
\^ # Literal caret (not start of line)
\$ # Literal dollar sign (not end of line)
\[ # Literal opening bracket
\] # Literal closing bracket
\{ # Literal opening brace
\} # Literal closing brace
\( # Literal opening parenthesis
\) # Literal closing parenthesis
\| # Literal pipe (not OR)
\\ # Literal backslash
Common Patterns
Email Validation
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Phone Number (US)
\+?\d{1,3}[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
URL
https?://(?:[\w-]+\.)+[\w-]+(?:/[\w-./?%&=]*)?
IPv4 Address
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
IPv6 Address
(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}
Date (YYYY-MM-DD)
\d{4}-\d{2}-\d{2}
Date (MM/DD/YYYY)
\d{2}/\d{2}/\d{4}
Time (HH:MM:SS)
([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]
Credit Card (Basic)
\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}
Hex Color Code
#[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3})?
Username
^[a-zA-Z0-9_]{3,20}$
Password (Strong)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
HTML Tags
<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)
XML Tags
<([^>]+)>(.*?)<\/\1>
JSON Key
"[^"]+"\s*:
JSON String Value
:\s*"([^"\\]|\\.)*"
Practical Examples
Extract All Numbers
\d+(?:\.\d+)?
Example:
Text: The price is $19.99 and $25.50
Matches: "19.99", "25.50"
Extract All Emails
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Example:
Text: Contact us at [email protected] or [email protected]
Matches: "[email protected]", "[email protected]"
Extract All URLs
https?://[^\s/$.?#].[^\s]*
Example:
Text: Visit https://example.com/page or http://test.org
Matches: "https://example.com/page", "http://test.org"
Remove Extra Whitespace
\s+
Replacement: Single space
Example:
Text: Hello world!
Result: Hello world!
Find Repeated Words
\b(\w+)\s+\1\b
Example:
Text: This is is a test test
Matches: "is is", "test test"
Match Quoted Strings
"[^"\\]|\\.)*"
Example:
Text: He said "hello" and "goodbye"
Matches: "hello", "goodbye"
Extract Hashtags
#\w+
Example:
Text: Check out #regex and #programming tips
Matches: "#regex", "#programming"
Extract Mentions
@\w+
Example:
Text: Thanks @user1 and @user2 for helping!
Matches: "@user1", "@user2"
Modifiers (Flags)
Case Insensitive
/i
Matches both uppercase and lowercase letters.
Multiline
/m
Makes ^ and $ match start/end of each line, not just string.
Global
/g
Find all matches, not just the first.
Dot Matches All
/s
Makes . match newline characters too.
Unicode
/u
Enable full Unicode support.
Sticky
/y
Matches only from the index indicated by lastIndex.
Lookarounds
Positive Lookahead
Match something only if it's followed by something else.
\w+(?=abc)
Matches word only if followed by "abc".
Negative Lookahead
Match something only if it's NOT followed by something else.
\w+(?!abc)
Matches word only if NOT followed by "abc".
Positive Lookbehind
Match something only if it's preceded by something else.
(?<=abc)\w+
Matches word only if preceded by "abc".
Negative Lookbehind
Match something only if it's NOT preceded by something else.
(?<!abc)\w+
Matches word only if NOT preceded by "abc".
Advanced Patterns
Validating Password Strength
# At least 8 characters, 1 uppercase, 1 lowercase, 1 digit
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
# At least 8 characters, 1 uppercase, 1 lowercase, 1 digit, 1 special
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&]).{8,}$
# At least 12 characters, mixed case, digit, special
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$
Extracting Domain from Email
@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})
Matching Balanced Parentheses
\(([^()]|(?R))*\)
(Recursive pattern, supported by some engines)
Validating Credit Card (Luhn Algorithm)
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9]{2})[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$
Matching HTML Attributes
([a-z-]+)=("[^"]*"|'[^']*')
Extracting CSS Values
([a-z-]+):\s*([^;]+)
Language-Specific Patterns
JavaScript
// Match JavaScript function
function\s+(\w+)\s*\([^)]*\)\s*\{[^}]*\}
// Match JavaScript variable
var\s+(\w+)\s*=\s*([^;]+)
// Match JavaScript object property
\w+\.\w+
Python
# Match Python function
def\s+(\w+)\s*\([^)]*\)\s*:.*?(?=\ndef\s|\Z)
# Match Python import
^import\s+([a-zA-Z0-9_]+)
# Match Python class
class\s+(\w+).*?(?=\nclass\s|\Z)
Java
// Match Java method
(?:public|private|protected)?\s*(?:static)?\s*(?:\w+\s+)+(\w+)\s*\([^)]*\)\s*\{[^}]*\}
// Match Java class
class\s+(\w+).*?{[^}]*}
CSS
/* Match CSS selector */
[a-z-]+\s*\{[^}]*\}
/* Match CSS property */
[a-z-]+\s*:\s*[^;]+;
/* Match CSS class selector */
\.[a-z-]+
SQL
/* Match SQL SELECT */
SELECT\s+[^;]+FROM\s+[^(;]+;
/* Match SQL table name */
FROM\s+([a-zA-Z_][a-zA-Z0-9_]*)
Optimization Tips
Use Character Classes Instead of OR
# Slower
a|b|c|d|e
# Faster
[a-e]
Avoid Catastrophic Backtracking
# Dangerous - can cause exponential backtracking
(a+)+
# Better - more specific
a{1,100}
Use Possessive Quantifiers (When Supported)
# Can backtrack
a.*ab
# No backtracking - faster
a.*+ab
Anchor Your Patterns
# Slower - searches entire string
pattern
# Faster - starts at beginning
^pattern
# Faster - ends at end
pattern$
Be Specific
# Too general
.*
# Better - specific characters
[a-zA-Z0-9]+
Common Tasks
Validate Input
# Email validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
# Phone number validation
^\+?\d{1,3}[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
# Date validation (YYYY-MM-DD)
^\d{4}-\d{2}-\d{2}$
Extract Data
# Extract URLs
https?://[^\s/$.?#].[^\s]*
# Extract emails
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
# Extract numbers
\d+(?:\.\d+)?
Clean Text
# Remove HTML tags
<[^>]*>
# Remove extra whitespace
\s+
# Remove special characters
[^a-zA-Z0-9\s]
Find and Replace
# Replace multiple spaces with single
\s+
→ single space
# Replace phone number format
(\d{3})[-.](\d{3})[-.](\d{4})
→ ($1) $2-$3
# Replace camelCase with snake_case
([a-z])([A-Z])
→ $1_$2
Quick Tips
- Start simple - Begin with literal characters, then add complexity
- Test frequently - Use our Regex Tester to verify patterns
- Use anchors - Pin your patterns to start/end of strings
- Escape special characters - Use
\before.*+?^$[]{}()|\ - Consider performance - Avoid nested quantifiers and excessive backtracking
- Use character classes - More efficient than multiple OR conditions
- Document complex patterns - Add comments when supported
- Use lazy quantifiers - When extracting multiple items
- Use greedy quantifiers - When capturing maximum content
- Practice regularly - Regex takes time and practice to master
Common Errors
Forgot to Escape Special Characters
# Wrong - matches any character
.test
# Correct - matches literal dot
\.test
Overmatching with Greedy Quantifiers
# Wrong - matches too much
<div>.*</div>
# Correct - matches individual tags
<div>.*?</div>
Not Using Anchors
# Wrong - matches anywhere in text
test
# Correct - matches only at start
^test
Catastrophic Backtracking
# Dangerous
(a+)+b
a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a
# Better
a{1,100}b
a{20}
Testing and Debugging
Use Our Interactive Regex Tester
- Paste your test text
- Enter your regex pattern
- See matches highlighted in real-time
- Adjust and iterate quickly
Debugging Checklist
- Did you escape special characters?
- Are you using the right quantifiers?
- Did you use anchors where needed?
- Is the pattern case-sensitive?
- Are you matching what you expect?
- Is performance acceptable?
Common Debugging Steps
- Simplify the pattern - Test each component separately
- Use character classes - Replace complex patterns with
[a-z]etc. - Add anchors - Start with
^and end with$to see full match - Test incremental - Build up complexity gradually
- Check for typos - Verify special characters and escapes
Resources
Online Tools
- Regex Tester - Interactive testing and debugging
- Regex Explainer - Understand complex patterns
- Regex Generator - Create patterns from examples
Practice
- Test patterns with real-world data
- Practice common validation scenarios
- Build regex for specific use cases
- Share and learn from community examples
Further Learning
- Lookarounds and advanced techniques
- Performance optimization
- Language-specific features
- Integration with programming languages
Summary
This cheat sheet covers the most common regex patterns, metacharacters, and use cases. Remember:
- Character classes match sets of characters
- Quantifiers control repetition
- Anchors define position
- Groups organize and capture
- Lookarounds assert context
- Escape special characters with backslash
Practice regularly, test thoroughly, and don't be afraid to experiment. With time and experience, you'll become proficient at crafting powerful and efficient regex patterns!
Bookmark this page for quick reference, and use our interactive tools to practice and perfect your regex skills!
About the Author
The Regex Master Team consists of experienced developers and technical writers dedicated to simplifying regular expressions for everyone. We ensure all patterns are rigorously tested and verified to provide accurate, production-ready solutions.
Try It: Regex Tester
Use our interactive regex tester to experiment with the patterns you learned in this article. Test your regular expressions in real-time and see immediate results.