Last updated
Regex Pattern Generator Examples
The Regex Pattern Generator creates regular expression patterns for common validation and matching tasks. Below are examples of generated patterns with explanations.
Email Validation Pattern
Generate a practical email validation regex:
Generated pattern (JavaScript):
/^[\w.\-+]+@[\w\-]+\.[a-zA-Z]{2,}$/
Explanation:
^ — start of string
[\w.\-+]+ — one or more word chars, dots, hyphens, or plus signs (local part)
@ — literal @ symbol
[\w\-]+ — one or more word chars or hyphens (domain name)
\. — literal dot
[a-zA-Z]{2,}— two or more letters (TLD)
$ — end of string
Matches:
user@example.com ✓
first.last@domain.org ✓
user+tag@sub.domain.co ✓
Rejects:
@example.com ✗ (no local part)
user@ ✗ (no domain)
user@domain ✗ (no TLD)
URL Validation Pattern
Generate a URL matching pattern:
Generated pattern:
/^https?:\/\/[\w\-]+(\.[\w\-]+)+(\/[\w\-._~:/?#[\]@!$&'()*+,;=%]*)?$/
Matches:
https://example.com ✓
http://sub.domain.org/path ✓
https://example.com/path?q=1#s ✓
Rejects:
ftp://example.com ✗
example.com ✗ (no protocol)
US Phone Number Pattern
Generate a flexible US phone number pattern:
Generated pattern:
/^\+?1?[\s.\-]?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$/
Matches all common US formats:
(415) 823-7491 ✓
415-823-7491 ✓
415.823.7491 ✓
4158237491 ✓
+1 415 823 7491 ✓
1-415-823-7491 ✓
Date Patterns
Generate date validation patterns for different formats:
ISO date (YYYY-MM-DD):
/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/
US date (MM/DD/YYYY):
/^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/
European date (DD.MM.YYYY):
/^(0[1-9]|[12]\d|3[01])\.(0[1-9]|1[0-2])\.\d{4}$/
Time (HH:MM or HH:MM:SS):
/^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/
IP Address Patterns
Generate IPv4 and IPv6 validation patterns:
IPv4:
/^(\d{1,3}\.){3}\d{1,3}$/
IPv4 with range validation (0-255):
/^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/
IPv6 (simplified):
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
Password Strength Pattern
Generate a strong password validation pattern using lookaheads:
Generated pattern (8+ chars, upper, lower, digit, special):
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
Explanation:
(?=.*[a-z]) — must contain at least one lowercase letter
(?=.*[A-Z]) — must contain at least one uppercase letter
(?=.*\d) — must contain at least one digit
(?=.*[@$!%*?&]) — must contain at least one special character
[A-Za-z\d@$!%*?&]{8,} — 8 or more allowed characters
Matches:
Secure@123 ✓
P@ssw0rd! ✓
Rejects:
password ✗ (no uppercase, digit, or special)
Password1 ✗ (no special character)
Credit Card Number Pattern
Generate a credit card validation pattern:
Visa (starts with 4, 16 digits):
/^4\d{15}$/
Mastercard (starts with 51-55 or 2221-2720):
/^(5[1-5]\d{14}|2(2[2-9]\d|[3-6]\d{2}|7[01]\d|720)\d{12})$/
American Express (starts with 34 or 37, 15 digits):
/^3[47]\d{13}$/
Any major card (with optional spaces/hyphens):
/^[\d\s\-]{13,19}$/
Hex Color Pattern
Generate a hex color code validation pattern:
Generated pattern:
/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
Matches:
#fff ✓ (3-digit shorthand)
#ffffff ✓ (6-digit full)
#1A2B3C ✓ (mixed case)
Rejects:
#gg0000 ✗ (invalid hex chars)
ffffff ✗ (missing #)
#12345 ✗ (wrong length)
Postal Code Patterns
Generate postal code patterns for different countries:
US ZIP code:
/^\d{5}(-\d{4})?$/
Matches: 90210, 90210-1234
UK postcode:
/^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i
Matches: SW1A 1AA, EC1A 1BB
Canadian postal code:
/^[A-Z]\d[A-Z]\s?\d[A-Z]\d$/i
Matches: K1A 0A9, M5V3L9
German PLZ:
/^\d{5}$/
Matches: 10115
Named Capture Groups for Data Extraction
Generate a log line parser with named groups:
Generated pattern:
/(?<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (?<level>INFO|WARN|ERROR) (?<message>.+)/
Input: "2026-03-17 14:23:01 ERROR Database connection failed"
Extracted:
timestamp: "2026-03-17 14:23:01"
level: "ERROR"
message: "Database connection failed"
JavaScript usage:
const match = line.match(pattern);
const { timestamp, level, message } = match.groups;
Pattern in Multiple Languages
The same email pattern generated for different languages:
JavaScript:
/^[\w.\-+]+@[\w\-]+\.[a-zA-Z]{2,}$/
Python:
import re
pattern = re.compile(r'^[\w.\-+]+@[\w\-]+\.[a-zA-Z]{2,}$')
Java:
Pattern pattern = Pattern.compile("^[\\w.\\-+]+@[\\w\\-]+\\.[a-zA-Z]{2,}$");
PHP (PCRE):
preg_match('/^[\w.\-+]+@[\w\-]+\.[a-zA-Z]{2,}$/', $email)
- Pre-built templates for email, URL, phone, date, IP, and more
- Describe custom formats and get the matching regex
- Test patterns against sample strings with real-time highlighting
- Named capture groups for structured data extraction
- Lookahead-based password strength validation
- Generate patterns for JavaScript, Python, Java, and PCRE
- Plain-English explanation of what each pattern matches
- Patterns designed to avoid catastrophic backtracking