“Regex for email” is one of the most-searched programming questions, and most answers are either dangerously naive (.*@.*) or spectacularly over-engineered (6,000-character RFC monsters that still don’t prove anything). This guide takes the pragmatic middle: what to actually put in your form validation, why, and what regex can never do for you.
You can test every pattern below live in the Regex Tester — matches highlight as you type, and nothing you paste is uploaded.
The pragmatic pattern
For a signup form, this is the pattern we recommend:
^[^\s@]+@[^\s@]+\.[^\s@]+$
Read it as: some non-space, non-@ characters, then @, then more of them, then a dot, then a non-empty tail. It accepts ada@example.com, rejects ada@, @example.com, ada example@x.com, and ada@example (no dot).
Why so loose? Because the goal of form validation is catching typos, not enforcing the RFC:
user+tag@gmail.com— valid, must accept (plus-addressing is common).user@mail.co.uk— valid, multi-part TLDs are everywhere.user@localhost— technically valid on intranets; your public signup form may reasonably reject it.用户@例子.广告— valid internationalized addresses; the pattern above accepts them, ASCII-only patterns wrongly reject them.
The full breakdown, with tested match/non-match lists and language-specific versions, lives on the email regex reference page.
What the RFC actually allows (and why you shouldn’t care)
RFC 5322 permits gems like:
"quoted spaces"@example.com— quoted local parts with spacesuser@[192.168.1.1]— IP-literal domainsa!#$%&'*+/=?^_`{|}~b@example.com— a zoo of special characters- Comments in parentheses, folded whitespace, and more
The famous “fully RFC-compliant” regex runs to thousands of characters, is unreadable, and differs between regex flavors. Worse, being RFC-compliant still doesn’t tell you whether nobody@example.com can receive mail. The complexity buys you almost nothing.
If you need strict structural validation, don’t write a bigger regex — use your platform’s address parser:
from email.utils import parseaddr
name, addr = parseaddr(candidate)
ok = "@" in addr and addr == candidate.strip()
// Browser-native, follows the HTML spec's willfully-valid rule
const input = document.createElement("input");
input.type = "email";
input.value = candidate;
const ok = input.checkValidity();
The HTML spec’s email input pattern is deliberately a willful violation of RFC 5322 — the WHATWG chose pragmatism over completeness, which tells you something about the trade-off.
Common mistakes that reject real users
- Hard-coded TLD lists.
(com|net|org)died years ago; there are 1,500+ TLDs including.dev,.io,.photography. Never enumerate them. - Length limits that are too tight. Local part ≤64 octets, total ≤254 octets is the real limit — not 30 characters.
- Rejecting
+and.in the local part. Plus-addressing and dotted names are standard Gmail features. - Case-folding the whole address. The domain is case-insensitive; the local part is technically case-sensitive. Lowercase the domain, leave the local part alone (in practice almost nobody’s mail server distinguishes, but don’t corrupt data on purpose).
- Anchors missing. Without
^…$(or full-match APIs),bad!!user@example.compartially matches and sneaks throughre.search.
Regex flavors: same pattern, different escaping
The pragmatic pattern needs small adjustments per language:
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
re.test(value);
import re
re.fullmatch(r"[^\s@]+@[^\s@]+\.[^\s@]+", value)
var re = regexp.MustCompile(`^[^\s@]+@[^\s@]+\.[^\s@]+$`)
re.MatchString(value)
value.matches("^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$");
Note Java’s double backslashes (string escaping) and Python’s fullmatch (no anchors needed). When a pattern “works on regex101 but not in my code”, the flavor or the escaping is almost always the reason — test in the Regex Tester with the JavaScript flavor, then translate deliberately.
The only validation that proves anything
Sending a confirmation email. Full stop.
- Syntax checks catch typos.
- MX-record lookups catch dead domains (but firewalls and greylisting make them unreliable).
- SMTP “verify” probes are widely blocked and considered abusive.
- A clicked confirmation link proves deliverability and ownership.
The production-grade pipeline is: cheap regex on the client for instant feedback → same check on the server (never trust the client) → confirmation email for proof. Anything more elaborate is procrastination with extra steps.
Extracting emails from text is a different job
Validation asks “is this whole string an address?” Extraction asks “find addresses inside this blob” — no anchors, word boundaries instead:
[\w.+-]+@[\w-]+\.[\w.-]+
Different pattern, different test cases. The email extraction reference page covers the boundaries and false positives (like trailing dots) in detail.
Summary
- Use
^[^\s@]+@[^\s@]+\.[^\s@]+$for forms; it catches typos without rejecting real users. - Skip the RFC monster; use platform parsers if you need rigor.
- Never hard-code TLDs; never reject
+; mind your regex flavor’s escaping. - Confirm by email — it’s the only proof that matters.
Test your candidate pattern against your own edge cases in the Regex Tester before shipping, and browse the regex pattern library for dozens of other tested, explained patterns.