The Ultimate Regex Cheat Sheet

Interactive, searchable, comprehensive reference for regular expressions. Click any pattern to copy it.

0
Patterns
8
Categories
0
Examples
Match Any Character
.
Matches any single character except newline
Match Digit
\d
Equivalent to [0-9]
Match Word Char
\w
Equivalent to [a-zA-Z0-9_]
One or More
+
Matches preceding element 1+ times
Optional
?
Matches preceding element 0 or 1 time
Capture Group
(...)
Groups and captures matched text
⚙ Interactive Regex Tester
Type a regex pattern and test string to see matches highlighted in real time.
Matches will appear here...
Character Classes
Match specific types or sets of characters
PatternDescriptionExample
.Click to copy Matches any single character except newline (\n)
Input
cat, cot, cut
Pattern: c.t matches "cat", "cot", "cut"
\\dClick to copy Matches any digit character. Equivalent to [0-9]
Input
Order 123
Pattern: \d+ matches "123"
\\DClick to copy Matches any non-digit character. Equivalent to [^0-9]
Input
abc123xyz
Pattern: \D+ matches "abc" and "xyz"
\\wClick to copy Matches any word character (letter, digit, underscore). Equivalent to [a-zA-Z0-9_]
Input
hello_world!
Pattern: \w+ matches "hello_world"
\\WClick to copy Matches any non-word character. Equivalent to [^a-zA-Z0-9_]
Input
hello world!
Pattern: \W matches " " and "!"
\\sClick to copy Matches any whitespace character (space, tab, newline, etc.)
Input
hello world
Pattern: \s matches the space between words
\\SClick to copy Matches any non-whitespace character
Input
hello world
Pattern: \S+ matches "hello" and "world"
[abc]Click to copy Character set: matches any one character listed (a, b, or c)
Input
apple, banana, cherry
Pattern: [abc] matches "a", "b", "c"
[^abc]Click to copy Negated set: matches any character NOT listed
Input
apple
Pattern: [^abc] matches "p", "p", "l", "e"
[a-z]Click to copy Range: matches any lowercase letter from a to z
Input
HelloWorld
Pattern: [a-z] matches lowercase letters
[A-Z]Click to copy Range: matches any uppercase letter from A to Z
Input
HelloWorld
Pattern: [A-Z] matches "H" and "W"
[0-9]Click to copy Range: matches any digit from 0 to 9. Same as \d
Input
abc123def
Pattern: [0-9]+ matches "123"
[a-zA-Z0-9]Click to copy Combined range: matches any alphanumeric character
Input
Hello!123
Pattern: [a-zA-Z0-9]+ matches "Hello" and "123"
Anchors
Match positions within the string, not characters
PatternDescriptionExample
^Click to copy Matches the start of the string (or start of line with m flag)
Input
Hello world
Pattern: ^Hello matches "Hello" at start
$Click to copy Matches the end of the string (or end of line with m flag)
Input
Hello world
Pattern: world$ matches "world" at end
\\bClick to copy Word boundary: position between a word character and a non-word character
Input
cat scattered cattle
Pattern: \bcat\b matches only standalone "cat"
\\BClick to copy Non-word boundary: position NOT at a word boundary
Input
scattered
Pattern: \Bcat\B matches "cat" inside words
^...$Click to copy Exact match: anchoring both start and end ensures the entire string matches
Input
cat
Pattern: ^cat$ matches only the exact string "cat"
Quantifiers
Specify how many times the preceding element should be matched
PatternDescriptionExample
*Click to copy Matches 0 or more of the preceding element (greedy)
Input
aaab ab b
Pattern: a*b matches "aaab", "ab", "b"
+Click to copy Matches 1 or more of the preceding element (greedy)
Input
aaab ab b
Pattern: a+b matches "aaab" and "ab", not "b"
?Click to copy Matches 0 or 1 of the preceding element (optional)
Input
color colour
Pattern: colou?r matches both spellings
{n}Click to copy Matches exactly n occurrences of the preceding element
Input
123-456-7890
Pattern: \d{3} matches exactly 3 digits
{n,}Click to copy Matches n or more occurrences of the preceding element
Input
a aa aaa aaaa
Pattern: a{2,} matches 2 or more "a"s
{n,m}Click to copy Matches between n and m occurrences (inclusive)
Input
a aa aaa aaaa
Pattern: a{2,3} matches 2 to 3 "a"s
*?Click to copy Lazy version of *: matches as few characters as possible
Input
<b>bold</b>
Pattern: <.*?> matches each tag individually
+?Click to copy Lazy version of +: matches 1+ but as few as possible
Input
aaa
Pattern: a+? matches each "a" individually
??Click to copy Lazy version of ?: prefers matching 0 times
Input
ab aab
Pattern: a??b prefers not matching the "a"
*+ (possessive)Click to copy Possessive quantifier: like greedy but won't backtrack (not in all engines)
Input
aaab
Pattern: a*+b prevents catastrophic backtracking
Groups & Backreferences
Group patterns together and capture matched text for reuse
PatternDescriptionExample
(abc)Click to copy Capturing group: groups the pattern and captures the match for backreference
Input
foobar
Pattern: (foo)(bar) captures "foo" in $1 and "bar" in $2
(?:abc)Click to copy Non-capturing group: groups without capturing. Useful for applying quantifiers
Input
abcabc
Pattern: (?:abc){2} matches "abcabc" without capturing
(?<name>abc)Click to copy Named capturing group: captures the match with a name for clarity
Input
2024-01-15
Pattern: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
\\1Click to copy Backreference: matches the same text as previously matched by the 1st capturing group
Input
abcabc
Pattern: (abc)\1 matches "abcabc" (repeated)
\\k<name>Click to copy Named backreference: refers to a named group
Input
abcabc
Pattern: (?<word>abc)\k<word> matches repeated group
(a|b)Click to copy Alternation inside group: matches either a or b
Input
cat and dog
Pattern: (cat|dog) matches "cat" or "dog"
👁
Lookaround Assertions
Match based on what comes before or after, without including it in the match
PatternDescriptionExample
(?=...)Click to copy Positive lookahead: asserts that what follows matches the pattern
Input
foobar foobaz
Pattern: foo(?=bar) matches "foo" only when followed by "bar"
(?!...)Click to copy Negative lookahead: asserts that what follows does NOT match
Input
foobar foobaz
Pattern: foo(?!bar) matches "foo" NOT followed by "bar"
(?<=...)Click to copy Positive lookbehind: asserts that what precedes matches the pattern
Input
$100 and 200
Pattern: (?<=\$)\d+ matches digits after "$"
(?<!...)Click to copy Negative lookbehind: asserts that what precedes does NOT match
Input
$100 and 200
Pattern: (?<!\$)\b\d+ matches digits NOT after "$"
Flags / Modifiers
Change the behavior of the regex engine
FlagDescriptionExample
gClick to copy Global: Find all matches, not just the first one
Without g
cat cat cat
With g
cat cat cat
iClick to copy Case Insensitive: Match regardless of uppercase/lowercase
Input
Hello HELLO hello
Pattern: /hello/gi matches all variations
mClick to copy Multiline: ^ and $ match start/end of each line, not just the string
Input
first line\nsecond line
Pattern: /^.+$/gm matches each line
sClick to copy DotAll (Single Line): Makes . also match newline characters
Input
line1\nline2
Pattern: /.+/s matches across newlines
uClick to copy Unicode: Enables full Unicode matching including surrogate pairs
Input
Hello 😀
Pattern: /\u{1F600}/u matches the emoji
yClick to copy Sticky: Matches only from the lastIndex position (anchored to position)
Input
abcdefabc
Pattern: /abc/y matches only at current position
Special Characters & Escaping
Special sequences and how to escape metacharacters
PatternDescriptionExample
\\Click to copy Escape character: treat the next metacharacter as literal
Input
Price: $9.99
Pattern: \$\d+\.\d+ matches "$9.99"
\\tClick to copy Matches a horizontal tab character
Matches the tab character (ASCII 9)
\\nClick to copy Matches a newline (line feed) character
Matches newline / line feed (ASCII 10)
\\rClick to copy Matches a carriage return character
Matches carriage return (ASCII 13). Windows line endings: \r\n
\\0Click to copy Matches a null character (ASCII 0)
Useful in binary data processing
\\uFFFFClick to copy Matches a Unicode character by its hex code point
Input
© 2024
Pattern: \u00A9 matches the copyright symbol ©
metacharactersClick to copy Characters that need escaping when used literally: . * + ? ^ $ { } [ ] ( ) | \
Prefix with \ to match literally: \. matches a period
Logic & Alternation
Combine patterns with OR logic and conditional matching
PatternDescriptionExample
a|bClick to copy Alternation (OR): matches either a or b
Input
cat and dog
Pattern: cat|dog matches "cat" or "dog"
(a|b|c)Click to copy Grouped alternation: matches any of multiple alternatives
Input
red green blue pink
Pattern: (red|green|blue)
(?(1)yes|no)Click to copy Conditional: if group 1 matched, try "yes" pattern, else "no" (PCRE/Python, not JS)
Engine-specific. Not available in JavaScript.
Common Real-World Patterns
Ready-to-use regex patterns for everyday validation and extraction tasks
Use CasePatternExample
Email Address [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}Click to copy
Matches
user@example.com
Also: name+tag@sub.domain.org
URL (HTTP/HTTPS) https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(...)Click to copy
Matches
https://www.example.com/path?q=1
Phone (US) (\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}Click to copy
Matches
(555) 123-4567, 555.123.4567, +1-555-123-4567
Phone (International) \+?[1-9]\d{1,14}Click to copy
Matches
+441234567890, +8613812345678
E.164 international phone format
IPv4 Address (?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|...)Click to copy
Matches
192.168.1.1, 10.0.0.255
Validates 0-255 range per octet
IPv6 Address (simplified) ([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}Click to copy
Matches
2001:0db8:85a3:0000:0000:8a2e:0370:7334
Date (YYYY-MM-DD) \d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])Click to copy
Matches
2024-01-15, 2023-12-31
ISO 8601 date format
Date (MM/DD/YYYY) (0[1-9]|1[0-2])\/...\/\d{4}Click to copy
Matches
01/15/2024, 12/31/2023
Time (HH:MM:SS) ([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?Click to copy
Matches
14:30, 23:59:59, 00:00:00
Hex Color Code #([0-9a-fA-F]{3}){1,2}\bClick to copy
Matches
#fff, #FF5733, #a1b2c3
HTML Tag <\/?[\w\s]*>|<.+[\W]>Click to copy
Matches
<div>, </p>, <img src="...">
Strong Password ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[...]{8,}$Click to copy
Matches
MyP@ss1word
Min 8 chars, upper, lower, digit, special
Username ^[a-zA-Z0-9_-]{3,20}$Click to copy
Matches
user_name-123
3-20 alphanumeric, underscore, hyphen
US Zip Code \b\d{5}(-\d{4})?\bClick to copy
Matches
90210, 90210-1234
Credit Card (basic) \b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\bClick to copy
Matches
4111 1111 1111 1111, 4111-1111-1111-1111
SSN (US) \b\d{3}-\d{2}-\d{4}\bClick to copy
Matches
123-45-6789
URL Slug ^[a-z0-9]+(?:-[a-z0-9]+)*$Click to copy
Matches
my-blog-post-title
URL-friendly slugs
File Extension \.[a-zA-Z0-9]{1,10}$Click to copy
Matches
document.pdf, image.png
Number with Commas \b\d{1,3}(,\d{3})*(\.\d+)?\bClick to copy
Matches
1,234,567.89, 100
Trim Whitespace ^\s+|\s+$Click to copy
Input
hello world
Matches leading/trailing whitespace for removal
Duplicate Words \b(\w+)\s+\1\bClick to copy
Matches
the the quick brown fox
Finds accidentally repeated words
MAC Address ([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}Click to copy
Matches
00:1A:2B:3C:4D:5E
🔍

No patterns match your search. Try different keywords.