Regex Cheat Sheet
A comprehensive reference guide for Regular Expressions (regex) with patterns, examples, and detailed explanations. Perfect for developers, data analysts, and anyone working with text processing.
literal text
BasicMatch exact characters
Pattern:
text
Examples:
hello
Matches the exact string 'hello'123
Matches the exact number '123'Hello World
Matches 'Hello World' exactly (case sensitive)Notes:
Most characters match themselves literally
. (dot)
BasicMatch any single character except newline
Pattern:
.
Examples:
h.llo
Matches 'hello', 'hallo', 'h3llo', etc.a.b
Matches 'axb', 'a b', 'a5b', etc.....
Matches any 4 charactersNotes:
Use \. to match a literal dot
\
BasicEscape special characters
Pattern:
\character
Examples:
\.
Matches a literal dot\$
Matches a literal dollar sign\\
Matches a literal backslash\(
Matches a literal opening parenthesisNotes:
Backslash removes special meaning from metacharacters
[abc]
Character ClassesMatch any one character from the set
Pattern:
[characters]
Examples:
[abc]
Matches 'a', 'b', or 'c'[0-9]
Matches any digit from 0 to 9[a-zA-Z]
Matches any letter (upper or lowercase)[aeiou]
Matches any vowelNotes:
Square brackets define a character class
[^abc]
Character ClassesMatch any character NOT in the set
Pattern:
[^characters]
Examples:
[^abc]
Matches any character except 'a', 'b', or 'c'[^0-9]
Matches any non-digit character[^aeiou]
Matches any consonant[^\s]
Matches any non-whitespace characterNotes:
Caret ^ at the beginning negates the character class
\d
Character ClassesMatch any digit (0-9)
Pattern:
\d
Examples:
\d
Matches '0', '1', '2', ..., '9'\d\d
Matches any two-digit number\d+
Matches one or more digitsNotes:
Equivalent to [0-9]
\D
Character ClassesMatch any non-digit character
Pattern:
\D
Examples:
\D
Matches 'a', 'B', '!', ' ', etc.\D+
Matches one or more non-digit characters\d\D
Matches a digit followed by a non-digitNotes:
Equivalent to [^0-9]
\w
Character ClassesMatch any word character (letters, digits, underscore)
Pattern:
\w
Examples:
\w
Matches 'a', 'Z', '5', '_'\w+
Matches one or more word characters\w{3}
Matches exactly 3 word charactersNotes:
Equivalent to [a-zA-Z0-9_]
\W
Character ClassesMatch any non-word character
Pattern:
\W
Examples:
\W
Matches '!', '@', ' ', '-', etc.\w+\W
Matches word characters followed by non-word character\W+
Matches one or more non-word charactersNotes:
Equivalent to [^a-zA-Z0-9_]
\s
Character ClassesMatch any whitespace character
Pattern:
\s
Examples:
\s
Matches space, tab, newline, etc.\s+
Matches one or more whitespace characters\w+\s+\w+
Matches two words separated by whitespaceNotes:
Includes space, tab, newline, carriage return, form feed
\S
Character ClassesMatch any non-whitespace character
Pattern:
\S
Examples:
\S
Matches any visible character\S+
Matches one or more non-whitespace characters\S\s\S
Matches non-space, space, non-spaceNotes:
Any character except whitespace
+
QuantifiersMatch one or more of the preceding character
Pattern:
pattern+
Examples:
a+
Matches 'a', 'aa', 'aaa', etc.\d+
Matches one or more digits[a-z]+
Matches one or more lowercase lettersNotes:
Greedy quantifier - matches as many as possible
*
QuantifiersMatch zero or more of the preceding character
Pattern:
pattern*
Examples:
a*
Matches '', 'a', 'aa', 'aaa', etc.\d*
Matches zero or more digits.*
Matches any characters (including empty string)Notes:
Greedy quantifier - matches as many as possible
?
QuantifiersMatch zero or one of the preceding character
Pattern:
pattern?
Examples:
colou?r
Matches 'color' or 'colour'\d?
Matches zero or one digithttps?
Matches 'http' or 'https'Notes:
Makes the preceding character optional
{n}
QuantifiersMatch exactly n occurrences
Pattern:
pattern{n}
Examples:
\d{3}
Matches exactly 3 digitsa{5}
Matches exactly 5 'a' characters[0-9]{4}
Matches exactly 4 digits (like a year)Notes:
Exact repetition count
{n,m}
QuantifiersMatch between n and m occurrences
Pattern:
pattern{n,m}
Examples:
\d{2,4}
Matches 2, 3, or 4 digitsa{1,3}
Matches 'a', 'aa', or 'aaa'[a-z]{3,6}
Matches 3 to 6 lowercase lettersNotes:
Range of repetitions
{n,}
QuantifiersMatch n or more occurrences
Pattern:
pattern{n,}
Examples:
\d{3,}
Matches 3 or more digitsa{2,}
Matches 'aa', 'aaa', 'aaaa', etc.\w{5,}
Matches 5 or more word charactersNotes:
Minimum repetition count
^
AnchorsMatch start of string/line
Pattern:
^pattern
Examples:
^Hello
Matches 'Hello' only at the beginning^\d+
Matches digits at the start of line^[A-Z]
Matches uppercase letter at beginningNotes:
Anchors the match to the beginning of the string or line
$
AnchorsMatch end of string/line
Pattern:
pattern$
Examples:
end$
Matches 'end' only at the end\d+$
Matches digits at the end of line\.$
Matches a period at the endNotes:
Anchors the match to the end of the string or line
\b
AnchorsMatch word boundary
Pattern:
\bpattern\b
Examples:
\bcat\b
Matches 'cat' as a whole word, not 'category'\btest
Matches 'test' at the beginning of a wording\b
Matches 'ing' at the end of a wordNotes:
Boundary between word (\w) and non-word (\W) characters
\B
AnchorsMatch non-word boundary
Pattern:
\Bpattern\B
Examples:
\Bcat\B
Matches 'cat' within a word, like in 'category'\Btest
Matches 'test' NOT at word boundarying\B
Matches 'ing' NOT at word endNotes:
NOT at word boundary - within a word
(pattern)
GroupsCreate capturing group
Pattern:
(pattern)
Examples:
(abc)+
Matches 'abc', 'abcabc', 'abcabcabc', etc.(\d{2})-(\d{2})-(\d{4})
Captures date parts: MM-DD-YYYY(Mr|Mrs|Ms)\.\s+(\w+)
Captures title and nameNotes:
Groups patterns and captures matched text for later use
(?:pattern)
GroupsCreate non-capturing group
Pattern:
(?:pattern)
Examples:
(?:abc)+
Groups 'abc' but doesn't capture(?:http|https)://
Matches protocol without capturing(?:Mr|Mrs|Ms)\.\s+(\w+)
Groups title but only captures nameNotes:
Groups patterns but doesn't create a capture group
|
GroupsMatch one pattern OR another
Pattern:
pattern1|pattern2
Examples:
cat|dog
Matches either 'cat' or 'dog'jpeg|jpg|png
Matches any of these image extensions(Mr|Mrs|Ms)
Matches any of these titlesNotes:
Logical OR operator - matches either side
(?=pattern)
LookaroundPositive lookahead - match if followed by pattern
Pattern:
pattern(?=lookahead)
Examples:
\w+(?=@)
Matches username before @ in email\d+(?=px)
Matches numbers followed by 'px'hello(?=\s+world)
Matches 'hello' only if followed by ' world'Notes:
Assertion - doesn't consume characters, just checks what follows
(?!pattern)
LookaroundNegative lookahead - match if NOT followed by pattern
Pattern:
pattern(?!lookahead)
Examples:
\w+(?!@)
Matches words NOT followed by @\d+(?!px)
Matches numbers NOT followed by 'px'hello(?!\s+world)
Matches 'hello' NOT followed by ' world'Notes:
Assertion - matches only if NOT followed by the pattern
(?<=pattern)
LookaroundPositive lookbehind - match if preceded by pattern
Pattern:
(?<=lookbehind)pattern
Examples:
(?<=@)\w+
Matches domain after @ in email(?<=\$)\d+
Matches numbers preceded by $(?<=hello\s)\w+
Matches word after 'hello 'Notes:
Assertion - checks what comes before the match
(?<!pattern)
LookaroundNegative lookbehind - match if NOT preceded by pattern
Pattern:
(?<!lookbehind)pattern
Examples:
(?<!@)\w+
Matches words NOT preceded by @(?<!\$)\d+
Matches numbers NOT preceded by $(?<!hello\s)\w+
Matches word NOT after 'hello 'Notes:
Assertion - matches only if NOT preceded by the pattern
Email
Common PatternsMatch email addresses
Pattern:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Examples:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Basic email pattern\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
Email with word boundariesNotes:
Basic email validation - not RFC compliant but covers most cases
URL
Common PatternsMatch URLs
Pattern:
https?://[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
Examples:
https?://\w+\.\w+
Simple URL patternhttps?://[\w\-]+(\.[\w\-]+)+
More robust URL patternNotes:
Matches HTTP and HTTPS URLs
Phone Number
Common PatternsMatch phone numbers
Pattern:
\(?\d{3}\)?[\s\-]?\d{3}[\s\-]?\d{4}
Examples:
\d{3}-\d{3}-\d{4}
Format: 123-456-7890\(\d{3}\)\s\d{3}-\d{4}
Format: (123) 456-7890\(?\d{3}\)?[\s\-]?\d{3}[\s\-]?\d{4}
Flexible US phone formatNotes:
US phone number formats
IP Address
Common PatternsMatch IPv4 addresses
Pattern:
\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b
Examples:
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
Simple IP pattern\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b
IP with word boundaries\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
Valid IP range (0-255)Notes:
Basic IP pattern - doesn't validate ranges
Date
Common PatternsMatch date formats
Pattern:
\d{1,2}/\d{1,2}/\d{4}
Examples:
\d{1,2}/\d{1,2}/\d{4}
MM/DD/YYYY or M/D/YYYY\d{4}-\d{2}-\d{2}
YYYY-MM-DD format\b\d{1,2}[-/]\d{1,2}[-/]\d{4}\b
Flexible date with - or /Notes:
Common date formats - doesn't validate actual dates
Credit Card
Common PatternsMatch credit card numbers
Pattern:
\d{4}[\s\-]?\d{4}[\s\-]?\d{4}[\s\-]?\d{4}
Examples:
\d{16}
16 consecutive digits\d{4}\s\d{4}\s\d{4}\s\d{4}
Groups of 4 with spaces\d{4}[\s\-]?\d{4}[\s\-]?\d{4}[\s\-]?\d{4}
Flexible spacing/dashesNotes:
Basic credit card format - doesn't validate card numbers
Hex Color
Common PatternsMatch hexadecimal color codes
Pattern:
#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3}
Examples:
#[0-9A-Fa-f]{6}
6-digit hex color: #FF0000#[0-9A-Fa-f]{3}
3-digit hex color: #F00#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3}
Both 3 and 6 digit hex colorsNotes:
Hexadecimal color codes with #
i
FlagsCase insensitive matching
Pattern:
/pattern/i
Examples:
/hello/i
Matches 'hello', 'Hello', 'HELLO', etc./[a-z]+/i
Matches letters regardless of case/test/i
Matches 'test', 'Test', 'TEST', etc.Notes:
Ignores case when matching
g
FlagsGlobal matching (find all matches)
Pattern:
/pattern/g
Examples:
/\d+/g
Finds all numbers in text/cat/g
Finds all occurrences of 'cat'/[aeiou]/g
Finds all vowelsNotes:
Without 'g', only first match is returned
m
FlagsMultiline mode (^ and $ match line breaks)
Pattern:
/pattern/m
Examples:
/^\w+/m
Matches word at start of each line/\w+$/m
Matches word at end of each line/^#.+$/m
Matches lines starting with #Notes:
Makes ^ and $ match start/end of lines, not just string
s
FlagsDot matches newline characters
Pattern:
/pattern/s
Examples:
/.*/ vs /.*/s
With 's', dot matches newlines too/<.*>/s
Matches tags across multiple linesNotes:
Makes . match newline characters (\n)
Regex Pro Tips
π§ͺ Test Your Patterns
Use our interactive regex tester to validate and experiment with patterns in real-time.
Common Workflow
- Start with literal text:
hello
- Add character classes:
[a-z]
- Use quantifiers:
+
,*
,?
- Add anchors if needed:
^
,$
- Test with different inputs
- Refine and optimize
Best Practices
- Keep patterns simple and readable
- Use
(?:)
for non-capturing groups - Escape special characters with
\
- Test edge cases and empty strings
- Consider performance for large datasets
- Document complex patterns with comments
Quick Reference
.
any char\
escape|
or*
0 or more+
1 or more?
0 or 1\d
digit\w
word char\s
whitespace^
start$
end\b
word boundaryπ What is Regex?
Regular Expressions (Regex) are powerful pattern-matching tools that allow you to search, match, and manipulate text using concise and flexible patterns. Originally developed in the 1950s and popularized in Unix tools like grep and sed, regex has become an essential skill for developers, data analysts, and anyone working with text processing. Think of regex as a sophisticated "find and replace" tool with superpowers.
π Key Features
- β Pattern matching: Find complex text patterns with simple expressions
- β Text validation: Verify formats like emails, phone numbers, URLs
- β Data extraction: Pull specific information from large text files
- β Search & replace: Advanced find/replace operations with captured groups
- β Cross-platform: Supported in virtually every programming language
- β Concise syntax: Express complex patterns in short, readable forms
π‘ Why Use Regex?
- β’ Efficiency: Process thousands of lines of text in milliseconds
- β’ Precision: Match exact patterns with incredible specificity
- β’ Automation: Automate repetitive text processing tasks
- β’ Data cleaning: Standardize and sanitize messy data formats
- β’ Log analysis: Extract insights from server logs and reports
- β’ Form validation: Ensure user input meets required formats
π― Common Use Cases
Web Development
Email validation, URL parsing, form input sanitization, and user data validation
Data Analysis
Log file parsing, CSV data cleaning, extracting structured information from unstructured text
System Administration
Configuration file management, log monitoring, automated text processing scripts
π How Regex Works
Pattern Building Blocks
- β’
a-z
Literal characters - β’
[0-9]
Character classes - β’
+*?
Quantifiers - β’
^$
Anchors - β’
()
Groups
Example Pattern
^\w+@[a-zA-Z_]+?\.[a-zA-Z]3$
Matches email addresses like: user@domain.com
ποΈ Regex Components
π Literals
Regular characters that match themselves exactly
hello
ποΈ Metacharacters
Special characters with special meanings
. ^ $ * +
π Quantifiers
Specify how many times to match
+ * ? {n}
π― Anchors
Match positions, not characters
^ $ \b
π Getting Started with Regex
1. Start Simple
Begin with literal text: cat
matches "cat" β’
Add a dot: c.t
matches "cat", "cot", "cut"
2. Learn the Basics
Master character classes [0-9]
β’
Understand quantifiers +*?
β’
Practice with anchors ^$
3. Practice & Test
Use online testers β’ Start with common patterns β’ Build complexity gradually β’ Test edge cases
βοΈ Regex Flavors
Popular Implementations
- β’ PCRE: Perl Compatible (PHP, Python)
- β’ ECMAScript: JavaScript standard
- β’ POSIX: Unix tools (grep, sed)
- β’ .NET: C#, VB.NET
- β’ Java: Built into Java platform
Key Differences
- β’ Lookahead/lookbehind support
- β’ Unicode property support
- β’ Escape sequence variations
- β’ Performance characteristics
- β’ Advanced features availability
Remember: Regex is like a superpowerβit can save you hours of manual work, but it takes practice to master. Start with simple patterns and gradually build complexity. This cheat sheet is your companion on the journey to regex mastery! π―