EUComply

Regex Examples — 20 Practical Patterns

Real-world regular expressions for validation, extraction, and text processing. Each pattern links to the live tester so you can experiment immediately.

Email address

^[\w\.\-]+@[\w\-]+\.\w{2,}$

Validates a basic email format — local part, @, domain, and TLD of at least 2 characters. Does not check whether the domain exists.

Use for: form validation, newsletter signups, contact forms.

Test this pattern →
Matches: [email protected], [email protected]
Rejects: @example.com, [email protected], user@com

URL / web address

https?:\/\/([\w\-]+\.)+[\w\-]+(\/[\w\-\.\/~%]*)?

Matches http and https URLs, with optional paths. Protocol is required to avoid false matches on plain words containing dots.

Use for: extracting links from text, link validation.

Test this pattern →
Matches: https://example.com, http://site.com/path/page.html
Rejects: example.com, ftp://files.example.com

Phone number

^\+?[\d\s\-\(\)]{7,15}$

Accepts international phone numbers with optional + prefix, spaces, hyphens, and parentheses. Length range covers most global formats.

Use for: international phone input fields, contact forms.

Test this pattern →
Matches: +1 555-123-4567, (44) 20 7946 0958, 1234567890
Rejects: 123 (too short), +abc-def-ghij

Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

Validates ISO-8601 date format with month (01–12) and day (01–31) range checking. Note: does not catch Feb 30 or leap-year edge cases — use a proper date parser for that.

Use for: date input validation, parsing ISO date strings.

Test this pattern →
Matches: 2026-08-25, 1999-12-31, 2024-02-29
Rejects: 2026-13-01, 2026-08-32, 2026-8-25

Time (HH:MM)

^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

Validates 24-hour time format. Hours 00–23, minutes 00–59.

Use for: time input in forms, meeting scheduler validation.

Test this pattern →
Matches: 08:30, 23:59, 00:00, 14:05
Rejects: 24:00, 12:60, 8:30, 12:5

IPv4 address

^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$

Matches valid IPv4 addresses where each octet is 0–255. Uses non-capturing groups (?:...) for cleaner matching.

Use for: network config validation, log parsing, IP extraction.

Test this pattern →
Matches: 192.168.1.1, 10.0.0.255, 8.8.8.8
Rejects: 256.1.2.3, 192.168.1.300, .1.2.3

IPv6 address (simplified)

^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$

Matches full (non-abbreviated) IPv6 addresses — 8 groups of 1–4 hex characters separated by colons. Does not cover :: shorthand.

Use for: basic IPv6 validation, network tools.

Test this pattern →
Matches: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Rejects: 2001:db8::1 (shorthand), 2001:xyz::1

Password strength (8+ chars, mixed)

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$

Requires at least 8 characters with at least one lowercase, one uppercase, one digit, and one special character (\W = non-word character). Uses lookaheads ((?=...)) for each requirement.

Use for: password strength meters, registration validation.

Test this pattern →
Matches: Passw0rd!, MyC4t$leeps, HelloWorld#1
Rejects: password, PASSWORD1, Pass1 (too short)

Username (alphanumeric + underscore)

^[a-zA-Z0-9_]{3,16}$

Alphanumeric characters and underscores only, 3–16 characters long. Common for forum signups, social handles, and developer usernames.

Use for: user registration, profile handles.

Test this pattern →
Matches: john_doe, DevGuy42, a_b_c
Rejects: abc (too short), user@name, a-very-long-username-here

Hex color code

^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$

Matches 3-digit and 6-digit hex color codes with # prefix. Case-insensitive for hex digits.

Use for: CSS color validation, color picker input.

Test this pattern →
Matches: #fff, #4361ee, #F0F, #aabbcc
Rejects: #abcd (4 chars), fff (no #), #gggggg

Credit card number

^\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}$

Matches 16-digit card numbers with optional spaces or hyphens every 4 digits. Use a Luhn check function for real validation — regex alone cannot detect valid card numbers.

Use for: payment form masking, basic format check.

Test this pattern →
Matches: 4111111111111111, 4242 4242 4242 4242, 4111-1111-1111-1111
Rejects: 123456789 (wrong length), abcd-efgh-ijkl-mnop

US ZIP code

^\d{5}(-\d{4})?$

Matches 5-digit ZIP codes with optional +4 extension.

Use for: address forms, shipping validation.

Test this pattern →
Matches: 94105, 90210-1234, 10001
Rejects: 1234 (too short), 123456, 94105-123

URL slug

^[a-z0-9]+(?:-[a-z0-9]+)*$

Lowercase alphanumeric slug with single hyphens between words. No leading/trailing hyphens, no double hyphens.

Use for: URL generation, blog post slugs, product page URLs.

Test this pattern →
Matches: hello-world, regex-tester, example, my-post-2026
Rejects: Hello-World, -leading, trailing-, double--hyphen

HTML tag

<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>

Matches an HTML open/close tag pair, capturing the tag name in group 1 and content in group 2. Uses the backreference \1 to match the closing tag name.

Use for: extracting HTML elements from markup, basic HTML parsing.

Test this pattern →
Matching on "<div class="main">Content</div>":
  Group 1: div
  Group 2: Content

#⃣ Number with decimals

^-?\d+(\.\d+)?$

Matches integers and decimal numbers, with optional negative sign. Requires at least one digit before and after the decimal point.

Use for: numeric input validation, price/amount parsing.

Test this pattern →
Matches: 42, -17, 3.14, -0.5, 100
Rejects: 3. (trailing dot), .5 (leading dot), 1,5 (wrong separator)

Domain name

^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$

Validates domain names: labels separated by dots, each label 1–63 characters, TLD at least 2 letters. Lowercase only.

Use for: domain validation, link parsing, email domain extraction.

Test this pattern →
Matches: example.com, sub.domain.co.uk, google.com
Rejects: example, .com, -example.com, example-.com

File extension

\.(pdf|docx?|xlsx?|pptx?|txt|zip|tar\.gz)$

Matches common file extensions at the end of a filename. Case-sensitive by default.

Use for: file type filtering, download link extraction.

Test this pattern →
Matches: report.pdf, cover-letter.docx, data.zip, archive.tar.gz
Rejects: file.pdfx, script.PDF (case mismatch), document

MAC address

^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$

Matches MAC addresses in colon-separated or hyphen-separated format. Each of the 6 groups is 2 hex digits.

Use for: network device identification, hardware validation.

Test this pattern →
Matches: 00:1A:2B:3C:4D:5E, 00-1A-2B-3C-4D-5E
Rejects: 00:1A:2B:3C:4D:5G, 001A2B3C4D5E (no separator), 00:1A:2B:3C:4D (5 groups)

SSN (US)

^\d{3}-\d{2}-\d{4}$

Matches the standard US Social Security Number format XXX-XX-XXXX. Does not validate SSN ranges or check against known invalid numbers.

Use for: form validation where SSN collection is required (HR, financial services).

Test this pattern →
Matches: 123-45-6789, 987-65-4321
Rejects: 123456789 (no hyphens), 12-345-6789 (wrong format), 123-45-678 (wrong length)

␣ Trim leading/trailing whitespace

^\s+|\s+$

Matches whitespace at the start or end of a string. In a replace operation, removing all matches trims the string. Use with the g (global) flag.

Use for: cleaning user input, normalizing strings, parsing CSV fields.

Test this pattern →
In "  hello world   " matches: "  " at start, "   " at end
After replacement: "hello world"

Regex Tester Pro

Save patterns, share with your team, use our AI regex explainer, and get syntax highlighting in every language. Just $2/month.

See Pro features →

How to use these patterns

Each pattern above links to the live Regex Tester with the pattern pre-loaded. You can:

  1. Click "Test this pattern" — opens the tester with the pattern ready
  2. Paste your own test data — see matches highlighted in real time
  3. Tweak the pattern — adjust it to fit your exact use case
  4. Use the Replace mode — test substitutions and transformations

Important: Regex validation is a first-pass filter, not a security boundary. Server-side validation is mandatory for any data that enters your system. These patterns are starting points — adapt them to your specific requirements.

When to avoid regex

Some problems look like a job for regex but are better solved with a dedicated parser: