Regex Cheat Sheet

Complete regular expression reference with interactive examples. Character classes, quantifiers, groups, lookahead, and 30+ ready-to-use patterns.

Quick Navigation

Try It Live

Type a regex pattern and test string below. Matches are highlighted in real time.

Try it: Change the pattern above to test any regex from this cheat sheet. For a full-featured tester, use our Regex Tester tool.

Basics & Anchors

PatternDescriptionExample
.Any character except newlinea.c matches "abc", "a1c"
^Start of string (or line with m flag)^Hello matches "Hello world"
$End of string (or line with m flag)end$ matches "the end"
\bWord boundary\bcat\b matches "cat" but not "catch"
\BNon-word boundary\Bcat matches "catch" but not "cat"
\Escape special character\. matches literal "."
|Alternation (OR)cat|dog matches "cat" or "dog"
Special characters that need escaping: . ^ $ * + ? { } [ ] \ | ( )

Character Classes

PatternDescriptionEquivalent
[abc]Match any one of a, b, or c
[^abc]Match anything except a, b, or c
[a-z]Match any lowercase letter
[A-Z]Match any uppercase letter
[0-9]Match any digit\d
[a-zA-Z0-9]Match any alphanumeric character
\dDigit[0-9]
\DNon-digit[^0-9]
\wWord character[a-zA-Z0-9_]
\WNon-word character[^a-zA-Z0-9_]
\sWhitespace (space, tab, newline)[ \t\n\r\f\v]
\SNon-whitespace[^ \t\n\r\f\v]

Quantifiers

PatternDescriptionExample
*0 or more (greedy)ab*c matches "ac", "abc", "abbc"
+1 or more (greedy)ab+c matches "abc", "abbc" but not "ac"
?0 or 1 (optional)colou?r matches "color" and "colour"
{n}Exactly n times\d{4} matches "2026"
{n,}n or more times\d{2,} matches "42", "123", "9999"
{n,m}Between n and m times\d{2,4} matches "42", "123", "9999"
*?0 or more (lazy / non-greedy)".*?" matches first quoted string
+?1 or more (lazy).+?; matches up to first semicolon
??0 or 1 (lazy)
Greedy vs lazy: By default, quantifiers are greedy (match as much as possible). Adding ? after makes them lazy (match as little as possible). Example: for the string "a" and "b", the pattern ".*" matches the entire string, while ".*?" matches "a" then "b" separately.

Groups & Backreferences

PatternDescriptionExample
(abc)Capturing group(ha)+ matches "haha"
(?:abc)Non-capturing group(?:ha)+ matches "haha" (no capture)
(?<name>abc)Named capturing group(?<year>\d{4})
\1Backreference to group 1(ha)\1 matches "haha"
\k<name>Backreference to named group(?<word>\w+)\s\k<word> matches repeated words
(a|b)Alternation within group(cat|dog)s matches "cats" or "dogs"

Useful group patterns

PatternWhat it does
(\w+)\s\1Find duplicate words ("the the")
(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})Named capture for date parts
(["']).*?\1Match quoted strings (matching quotes)

Lookahead & Lookbehind

Lookaround assertions match a position without consuming characters. They check what comes before or after the current position.

PatternNameDescription
(?=abc)Positive lookaheadFollowed by "abc"
(?!abc)Negative lookaheadNOT followed by "abc"
(?<=abc)Positive lookbehindPreceded by "abc"
(?<!abc)Negative lookbehindNOT preceded by "abc"

Practical examples

PatternWhat it matches
\d+(?= dollars)Numbers followed by " dollars" (matches "100" in "100 dollars")
\d+(?! dollars)Numbers NOT followed by " dollars"
(?<=\$)\d+Numbers preceded by "$" (matches "50" in "$50")
(?<!@)\w+Words NOT preceded by "@"
(?=.*[A-Z])(?=.*\d).{8,}Password with at least 1 uppercase and 1 digit, 8+ chars
Note: Lookbehind has limited support in some engines. JavaScript supports it in modern browsers (ES2018+). Python, Java, .NET, and PCRE all support it.

Flags (Modifiers)

g Global: find all matches
i Case-insensitive
m Multiline: ^ and $ match line boundaries
s Dotall: . matches newlines too
u Unicode: full Unicode support
y Sticky: match at lastIndex position only

Using flags in different languages

LanguageSyntax
JavaScript/pattern/gi or new RegExp("pattern", "gi")
Pythonre.findall(r"pattern", text, re.IGNORECASE | re.MULTILINE)
JavaPattern.compile("pattern", Pattern.CASE_INSENSITIVE)
Go(?i)pattern (inline flag)
PHP/pattern/gi
Ruby/pattern/im

Common Regex Patterns

Copy-paste ready patterns for common validation tasks.

Email Address
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Fails: user@, @domain.com, [email protected]
URL (HTTP/HTTPS)
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)
Matches: https://example.com, http://sub.domain.com/path?q=1
IPv4 Address
^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$
Matches: 192.168.1.1, 10.0.0.1, 255.255.255.0
Fails: 256.1.1.1, 1.2.3, 1.2.3.4.5
IPv6 Address (simplified)
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
Matches: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Phone Number (US)
^(\+1[-.\s]?)?(\(?\d{3}\)?[-.\s]?)?\d{3}[-.\s]?\d{4}$
Matches: (555) 123-4567, 555-123-4567, +1 555.123.4567
Phone Number (International E.164)
^\+[1-9]\d{1,14}$
Matches: +14155552671, +442071838750
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Matches: 2026-04-01, 2025-12-31
Fails: 2026-13-01, 2026-00-15
Time (HH:MM, 24-hour)
^([01]\d|2[0-3]):[0-5]\d$
Matches: 09:30, 23:59, 00:00
Fails: 24:00, 9:30 (needs leading zero)
Hex Color Code
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Matches: #fff, #6c63ff, #ABC123
Fails: #gg0000, fff (no #)
Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Minimum 8 chars, 1 uppercase, 1 lowercase, 1 digit, 1 special character
Username (alphanumeric + underscores, 3-16 chars)
^[a-zA-Z0-9_]{3,16}$
Matches: john_doe, User42, admin
Slug (URL-friendly string)
^[a-z0-9]+(-[a-z0-9]+)*$
Matches: my-page, regex-cheat-sheet, hello
Fails: My-Page, --double, trailing-
Credit Card Number (basic Luhn-compatible format)
^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$
Matches: 4111111111111111, 4111-1111-1111-1111, 4111 1111 1111 1111
SSN (US Social Security Number)
^\d{3}-\d{2}-\d{4}$
Matches: 123-45-6789
ZIP Code (US, 5 or 9 digit)
^\d{5}(-\d{4})?$
Matches: 90210, 90210-1234
HTML Tag
<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>
Matches opening and closing HTML tags with content. Not suitable for full HTML parsing.
Whitespace Trimming
^\s+|\s+$
Use with replace to trim leading and trailing whitespace
Duplicate Words
\b(\w+)\s+\1\b
Matches: "the the", "is is"
UUID v4
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
Matches: 550e8400-e29b-41d4-a716-446655440000
Semantic Version (semver)
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)?$
Matches: 1.0.0, 2.1.3, 1.0.0-beta.1
JSON String
"(?:[^"\\]|\\.)*"
Matches a properly escaped JSON string value

Test Your Regex Patterns

Use our full regex tester with real-time matching, capture groups, and multi-language support.

Open Regex Tester

Related Tools

Back to ToolPipe
Get a free API key for 100+ developer endpoints:
Pro plans
ToolPipe JSON Formatter CSS Minifier JS Minifier UUID Generator Regex Tester JWT Decoder Password Generator Hash Generator Base64 JSON to YAML QR Generator Merge PDF Image to Base64 Color Picker My IP XML Formatter YAML Validator CSV to JSON Diff Checker SQL Formatter Free API Key Pro Plans Quick Start
130+ free developer tools by ToolPipe. No signup, no tracking. Support us