Copy-paste regex patterns for validating emails, phone numbers, URLs, passwords, IPs, and dates. Includes comparison table with examples and language-specific syntax for JavaScript, Python, and C#.
Common Validation Patterns
Below is a reference table of commonly used regex patterns for field validation, originally sourced from the MSDN library and expanded with modern patterns.
| Field | Pattern | Example Match | Description |
|---|
| Email | ^[^\s@]+@[^\s@]+\.[^\s@]+$ | someone@example.com | Simple email validation covering 99% of real addresses |
| URL | ^https?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-\.~!$&'()*+,;=:@%]*$ | https://example.com/path | HTTP/HTTPS URLs with paths |
| Phone (US) | ^\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})$ | (425) 555-0123 | US phone with optional parentheses and separators |
| Phone (International) | ^\+?[1-9]\d{1,14}$ | +521234567890 | E.164 format, 1-15 digits |
| ZIP Code (US) | ^\d{5}(-\d{4})?$ | 12345 or 12345-6789 | 5-digit or ZIP+4 format |
| IPv4 Address | ^(\d{1,3}\.){3}\d{1,3}$ | 192.168.1.1 | Basic IPv4 (add range check 0-255 in code) |
| Date (YYYY-MM-DD) | ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ | 2026-02-28 | ISO 8601 date format |
| Password (strong) | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$ | P@ssw0rd! | Min 8 chars, uppercase, lowercase, digit, special |
| Non-negative integer | ^\d+$ | 0, 42, 986 | Zero or positive whole numbers |
| Currency | ^-?\d+(\.\d{2})?$ | 1.00, -25.50 | Positive or negative with 2 decimal places |
| SSN | ^\d{3}-\d{2}-\d{4}$ | 123-45-6789 | US Social Security Number format |
| Hex color | ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ | #FF5733, #F00 | CSS hex color codes |
| Slug | ^[a-z0-9]+(-[a-z0-9]+)*$ | my-blog-post | URL-friendly slugs |
| Username | ^[a-zA-Z0-9_]{3,20}$ | john_doe42 | 3-20 alphanumeric + underscore |
Language-Specific Syntax
| Language | Create Regex | Test Match | Find All | Replace |
|---|
| JavaScript | /pattern/flags | regex.test(str) | str.matchAll(/p/g) | str.replace(/p/g, 'r') |
| Python | re.compile(r'pattern') | re.match(p, str) | re.findall(p, str) | re.sub(p, 'r', str) |
| C# | new Regex(@"pattern") | regex.IsMatch(str) | regex.Matches(str) | regex.Replace(str, "r") |
| Character | Meaning | Example |
|---|
. | Any character except newline | a.c → abc, a1c |
\d | Any digit (0-9) | \d{3} → 123 |
\w | Word character (a-z, 0-9, _) | \w+ → hello_42 |
\s | Whitespace (space, tab, newline) | \s+ → matches spaces |
^ | Start of string | ^Hello |
$ | End of string | world$ |
* | Zero or more (greedy) | ab*c → ac, abc, abbc |
+ | One or more (greedy) | ab+c → abc, abbc |
? | Zero or one (optional) | colou?r → color, colour |
{n,m} | Between n and m times | \d{2,4} → 12, 123, 1234 |
() | Capture group | (\d+)-(\d+) |
(?=) | Lookahead | \d+(?=px) → 12 in “12px” |
Related Articles