DevBlacksmith

Tech blog and developer tools

Common Regex Patterns

EmailContact
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Standard email address validation
Examples: user@example.com, name+tag@mail.co
URLContact
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)$
HTTP/HTTPS URL validation
Examples: https://example.com, http://sub.domain.co/path?q=1
IPv4 AddressContact
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
IPv4 address (0.0.0.0 - 255.255.255.255)
Examples: 192.168.1.1, 10.0.0.255
IPv6 AddressContact
^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$
Full IPv6 address (no shorthand)
Examples: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Phone (International)Contact
^\+?[1-9]\d{1,14}$
E.164 international phone format
Examples: +5511999998888, +14155552671
Phone (BR)Contact
^\(?\d{2}\)?\s?9?\d{4}[-\s]?\d{4}$
Brazilian phone number
Examples: (11) 99999-8888, 11999998888
UUID v4Formats
^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
UUID version 4 format
Examples: 550e8400-e29b-41d4-a716-446655440000
CPFIdentity
^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$
Brazilian CPF (with or without dots/dash)
Examples: 123.456.789-09, 12345678909
CNPJIdentity
^\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}$
Brazilian CNPJ (with or without formatting)
Examples: 12.345.678/0001-90, 12345678000190
CEP (BR)Identity
^\d{5}-?\d{3}$
Brazilian ZIP code
Examples: 01001-000, 01001000
SSN (US)Identity
^\d{3}-?\d{2}-?\d{4}$
US Social Security Number
Examples: 123-45-6789, 123456789
Credit CardFormats
^(?: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})$
Visa, Mastercard, Amex, Discover (digits only)
Examples: 4111111111111111, 5500000000000004
Date (YYYY-MM-DD)Formats
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
ISO 8601 date format
Examples: 2025-01-15, 2024-12-31
Date (DD/MM/YYYY)Formats
^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$
Brazilian / European date format
Examples: 15/01/2025, 31/12/2024
Time (HH:MM:SS)Formats
^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$
24-hour time format
Examples: 14:30:00, 23:59
Hex ColorFormats
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$
CSS hex color code (3, 6 or 8 digits)
Examples: #fff, #3a7bd5, #00d2ffcc
MAC AddressFormats
^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$
Network MAC address
Examples: 00:1B:44:11:3A:B7, A1-B2-C3-D4-E5-F6
SlugFormats
^[a-z0-9]+(?:-[a-z0-9]+)*$
URL-friendly slug
Examples: my-blog-post, api-key-generator
Semantic VersionFormats
^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[a-zA-Z0-9.]+)?(\+[a-zA-Z0-9.]+)?$
SemVer (Major.Minor.Patch)
Examples: 1.0.0, v2.3.1-beta.1
JWT TokenSecurity
^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$
JSON Web Token (3 base64url parts)
Examples: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.abc123
Bearer Token HeaderSecurity
^Bearer\s+[A-Za-z0-9\-._~+/]+=*$
Authorization: Bearer <token>
Examples: Bearer eyJhbGciOiJIUzI1NiJ9...
API Key (prefix)Security
^(sk|pk|api|key|token)_(live|test|prod|dev)_[A-Za-z0-9]{16,}$
Common API key with prefix pattern
Examples: sk_live_abc123def456ghi789, api_test_XyZ123AbC456
Hex Key (256-bit)Security
^[0-9a-fA-F]{64}$
256-bit hex-encoded key (64 hex chars)
Examples: a1b2c3d4e5f6...
Base64 StringSecurity
^[A-Za-z0-9+/]+=*$
Standard Base64 encoded string
Examples: SGVsbG8gV29ybGQ=, dGVzdA==
Strong PasswordSecurity
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&#])[A-Za-z\d@$!%*?&#]{8,}$
Min 8 chars, uppercase, lowercase, number, symbol
Examples: P@ssw0rd!, MyS3cur3#Key
UsernameIdentity
^[a-zA-Z0-9_-]{3,20}$
Alphanumeric with underscores/dashes (3-20 chars)
Examples: john_doe, user-123
Domain NameContact
^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$
Valid domain name
Examples: example.com, sub.domain.co.uk

What Are Regular Expressions?

Regular expressions (regex) are patterns used to match character combinations in strings. The concept was introduced by mathematician Stephen Cole Kleene in 1951 as part of formal language theory. Ken Thompson brought regex into computing in 1968 when he implemented them in the QED text editor, which later influenced grep, one of the most important Unix commands ever created.

The name "grep" literally stands for "globally search for a regular expression and print matching lines" (g/re/p). Today, regex is built into virtually every programming language, text editor, and IDE. It is one of the most powerful tools in a developer's arsenal, though it has a reputation for being hard to read, leading to the famous Jamie Zawinski quote: "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems."

Fun fact: regex engines come in two main flavors. NFA (Nondeterministic Finite Automaton) engines, used by Perl, Python, and JavaScript, try every possible match path. DFA (Deterministic Finite Automaton) engines, used by grep and awk, are faster but less feature-rich. Some patterns can cause NFA engines to take exponential time, a vulnerability called ReDoS (Regular Expression Denial of Service).

Email Validation

The official regex for validating email addresses according to RFC 5322 is over 6,000 characters long. In practice, most developers use a simplified pattern because the full spec allows bizarre but valid addresses like "user@[IPv6:::1]".

Regex in Security

Web Application Firewalls (WAFs) use regex patterns to detect SQL injection, XSS attacks, and other threats. Security tools like Snort and ModSecurity maintain extensive regex rule sets. Many secret scanners also use regex to find leaked API keys in code.

Lookaheads and Lookbehinds

Advanced regex features like lookaheads (?=...) and lookbehinds (?<=...) let you match patterns based on what comes before or after without including it in the match. Password validation often uses lookaheads to check for multiple requirements simultaneously.

Online Regex Testers

Tools like regex101.com and regexr.com let you test patterns interactively with explanations. regex101 even shows you the match steps, which is invaluable for debugging complex patterns. These are essential tools in every developer's bookmarks.