Regular expressions (regex) are one of the most powerful tools in a developer's toolkit — and one of the most intimidating. In this guide, we break down the 10 most useful regex patterns that solve real-world problems with clear explanations and test examples.
Quick Regex Reference
Before diving into patterns, here's a quick syntax refresher:
| Symbol | Meaning | Example |
|---|---|---|
. |
Any character except newline | a.c → "abc", "a1c" |
* |
0 or more of previous | ab*c → "ac", "abc", "abbc" |
+ |
1 or more of previous | ab+c → "abc", "abbc" |
? |
0 or 1 of previous (optional) | colou?r → "color", "colour" |
\d |
Any digit [0-9] | \d{3} → "123", "456" |
\w |
Word character [a-zA-Z0-9_] | \w+ → "hello_world" |
\s |
Whitespace character | \s+ → spaces, tabs, newlines |
^ |
Start of string | ^Hello |
$ |
End of string | world$ |
() |
Capture group | (\d+)-(\d+) |
1. Email Validation
The most commonly needed regex pattern. This version handles most real-world email formats:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$Regex
Breakdown:
[a-zA-Z0-9._%+-]+— local part (before @): letters, numbers, dots, underscores, etc.@— literal @ symbol[a-zA-Z0-9.-]+— domain name\.[a-zA-Z]{2,}— TLD (.com, .io, .dev, etc.)
✅ Matches: [email protected], [email protected]
❌ Rejects: @missing.com, [email protected], user@com
2. URL Matching
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)Regex
Matches both HTTP and HTTPS URLs with optional www prefix, path, query string, and fragment.
✅ Matches: https://polymorpher.dev/json-to-classes, http://example.com?q=test
3. IPv4 Address
^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$Regex
Validates that each octet is between 0-255:
✅ Matches: 192.168.1.1, 10.0.0.255
❌ Rejects: 256.1.1.1, 192.168.1
4. Strong Password Validation
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$Regex
Requires:
- At least 8 characters
- At least one lowercase letter
- At least one uppercase letter
- At least one digit
- At least one special character (@$!%*?&)
✅ Matches: MyP@ss1word
❌ Rejects: password, 12345678, NoSpecial1
5. Phone Number (International)
^\+?[1-9]\d{1,14}$Regex
Follows the E.164 international phone number format (up to 15 digits with optional + prefix):
✅ Matches: +905551234567, 12025551234
6. Date Format (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$Regex
Validates ISO 8601 date format with proper month (01-12) and day (01-31) ranges:
✅ Matches: 2026-03-05, 2025-12-31
❌ Rejects: 2026-13-01, 2026-00-15
This regex validates the format but doesn't check calendar logic (e.g., Feb 30). Always combine regex validation with proper date parsing in your programming language. Use our DateTime Converter to test date formats.
7. Hex Color Code
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$Regex
Matches both 6-digit (#FF5733) and 3-digit (#F53) hex color codes:
✅ Matches: #FF5733, #fff, #0a0b0c
❌ Rejects: #GGG, FF5733, #12345
Convert hex colors with our Color Converter tool!
8. Extract HTML Tags
<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)<\/\1>Regex
Captures opening tag name (group 1) and inner content (group 2). Uses backreference \1 to match
the closing tag:
✅ Matches: <div>content</div>, <p class="intro">text</p>
Regex is fine for simple tag extraction, but never use it to parse complex HTML. Nested tags, self-closing elements, and edge cases make this unreliable. Use a proper HTML parser (DOMParser, BeautifulSoup, HtmlAgilityPack) for production code.
9. Credit Card Number (Basic)
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$Regex
Detects major card types:
4...— Visa (13 or 16 digits)51-55...— MasterCard (16 digits)34/37...— American Express (15 digits)6011/65...— Discover (16 digits)
10. Slug Generator (URL-Safe String)
[^a-zA-Z0-9]+Regex (for replacement)
Use this pattern with a replace operation to convert any string into a URL-safe slug:
// JavaScript
function slugify(text) {
return text
.toLowerCase()
.trim()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
}
slugify("Hello, World! 123");
// Result: "hello-world-123"JavaScript
C# Version
using System.Text.RegularExpressions;
string Slugify(string text) =>
Regex.Replace(text.ToLowerInvariant().Trim(),
@"[^a-z0-9]+", "-").Trim('-');
Slugify("Hello, World! 123");
// Result: "hello-world-123"C#
Try Regex Tester
Test all these patterns live against your own text. See matches, capture groups, and get instant feedback.
Open Regex Tester →Conclusion
These 10 patterns cover the most common regex needs — from validation (email, phone, passwords) to extraction (URLs, HTML tags) to transformation (slugification). Bookmark this page as your quick reference, and use Polymorpher's Regex Tester to experiment with patterns in real-time.
For related encoding and conversion work, check out the URL Encoder for percent-encoding, the Base64 tool for binary-to-text conversion, and the Hash Generator for checksum generation.