← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Common Regex Patterns

Regex PHP 5.3+ Beginner
debt(d7/e1/b3/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7) — regex101 helps test patterns manually and phpstan won't flag a buggy email regex; broken validation typically surfaces when real users with unusual addresses are rejected.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1) — quick_fix says replace custom regex with filter_var(FILTER_VALIDATE_EMAIL), a single-call swap.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3) — validation patterns tend to be sprinkled in form handlers and input layers; not architectural but more than a single utility, and copy-pasted regex tends to proliferate.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7) — the misconception (a regex matching most emails is 'good enough') contradicts reality: RFC 5321 email format is hundreds of lines and format validity says nothing about deliverability; the obvious DIY approach is reliably wrong.

About DEBT scoring →

Also Known As

email regex URL regex validation patterns

TL;DR

Practical regex patterns for email, URL, phone, IP address, dates, and identifiers — with caveats about when to use dedicated validators instead.

Explanation

Tempting but often wrong: email regex. RFC 5321 email addresses are almost impossible to fully validate with regex — use filter_var($email, FILTER_VALIDATE_EMAIL) in PHP. URL validation: parse_url() is more reliable than regex. Useful regex patterns: UUID validation, slug validation, credit card format (not validation), postal codes for specific countries. The key insight: regex validates format, not semantics — a regex-valid email may not exist.

Common Misconception

A regex that matches most emails is good enough for validation — email deliverability depends on the MX record, not format; use FILTER_VALIDATE_EMAIL for format and send a verification email for existence.

Why It Matters

Custom email and URL regex are notoriously buggy — PHP provides better built-in validators (filter_var) that handle edge cases the custom regex misses.

Common Mistakes

  • Custom email regex that rejects valid addresses — addresses with +, quoted strings, or long TLDs.
  • Using regex for format validation where PHP built-ins exist — filter_var(), parse_url(), ctype_* are tested and correct.
  • Copy-pasting regex without understanding — a pattern that works for one locale breaks for another.
  • URL regex that misses edge cases — IPv6 addresses, unicode domains, non-standard ports.

Code Examples

✗ Vulnerable
// Custom email regex — rejects valid addresses:
if (!preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $email)) {
    die('Invalid email');
}
// Rejects: user+tag@example.com ✗ (valid)
// Rejects: user@subdomain.co.uk ✓ (matches, OK)
// Rejects: user@xn--nxasmq6b.com ✗ (valid IDN)
✓ Fixed
// Use PHP built-ins:
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) die('Invalid email');
if (!filter_var($url, FILTER_VALIDATE_URL)) die('Invalid URL');

// Regex for things built-ins don't cover:
$uuidPattern = '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i';
$slugPattern = '/^[a-z0-9]+(?:-[a-z0-9]+)*$/';
$hexColour   = '/^#([0-9a-f]{3}|[0-9a-f]{6})$/i';

Added 15 Mar 2026
Edited 5 Apr 2026
Views 56
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 1 ping W 1 ping T 1 ping F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 3 pings F
Applebot 1 SEMrush 1 Brave Search 1
No pings yesterday
Ahrefs 6 Amazonbot 6 Perplexity 4 Scrapy 4 Google 3 Bing 3 SEMrush 3 Unknown AI 2 PetalBot 2 Meta AI 1 Twitter/X 1 Applebot 1 Brave Search 1
crawler 35 crawler_json 2
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
Regex general A regex (regular expression) is a pattern you write to search, match, or replace text. It's like a super-powered find-and-replace that can match flexible patterns instead of exact words.

Regex turns hours of manual text hunting into a single line of code. From form validation to log parsing to data cleanup, pattern matching is everywhere in real-world programming.

💡 When a regex misbehaves, check if your special characters need escaping — dots, brackets, and slashes often do.

Ask Codex about Regex →
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Use filter_var(FILTER_VALIDATE_EMAIL) for emails instead of regex — the RFC 5321 email regex is hundreds of lines long; built-in validators are battle-tested
📦 Applies To
PHP 5.3+ any web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Custom email regex that fails on valid addresses; URL regex missing edge cases; date regex not accounting for month lengths
Auto-detectable: ✗ No regex101 phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function
CWE-400


✓ schema.org compliant