Write a regex that matches an email address but rejects the obviously
The problem: one regex, a pile of bad addresses
Email validation looks easy until you actually test it. [email protected] passes, but so do user@@example.com, user [email protected], and user@example. A regex that accepts those three is not validating email, it is checking whether the string happens to contain an @. This article walks through a pattern that rejects the obvious garbage without breaking on legitimate addresses.
Start with a pattern that rejects the obvious mistakes
The smallest useful starting point is:
^[^\s@]+@[^\s@]+\.[^\s@]+$
Read it piece by piece:
^and$anchor the match so the whole string must be an address, not contain one.[^\s@]+before the @ rejects whitespace and a second @ in the local part.[^\s@]+\.[^\s@]+after the @ forces at least one dot and rejects whitespace in the domain.
This kills plainaddress, user@@example.com, and user [email protected] in one pass. It still lets through [email protected] and [email protected], so the next round tightens the domain.
Tighten the domain, then the local part
Require a real-looking domain label structure. A stricter ASCII version looks like:
^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$
What changes:
- The local part is restricted to characters RFC 5321 actually allows, including
.,+, and-. - Each domain label starts and ends with an alphanumeric character, with hyphens only in the middle.
-example.comandexample..comnow fail. - The
+after the first label requires at least one more label, so a bare TLD likeuser@localhostfails.
The trade-off is real: this pattern rejects some technically valid addresses (quoted local parts, internationalized domains) in exchange for much better precision on everyday inputs.
Test it against realistic examples

| Input | Should it pass? | Why |
|---|---|---|
[email protected] | Pass | Plus addressing is valid and common in filters. |
[email protected] | Pass | Multiple domain labels are fine. |
plainaddress | Fail | No @ at all. |
user@@example.com | Fail | Double @ in the local part. |
user [email protected] | Fail | Whitespace in the local part. |
user@example | Fail | No dot in the domain. |
[email protected] | Fail | Label starts with a hyphen. |
[email protected] | Fail | Empty label between dots. |
Use RegexToolBox to iterate instead of guessing
Copy the pattern, paste all eight inputs above, and watch which rows pass. That loop is the point: change one rule, re-run the whole table. RegexToolBox (https://regextoolbox.com/) lets you paste the regex and multiple test strings side by side, pick the flavor (JavaScript, Python, Java), and see matches and failures without switching windows. Verify its current flavor and flag options first, then use it for every draft of the pattern.
Common pitfalls that break real-world validation
- Rejecting plus addressing.
[email protected]is valid. A pattern that bans+will bounce newsletter signups. - Assuming TLDs are two or three letters.
.museum,.travel, and new gTLDs break[a-z]{2,3}$checks. - Over-engineering the local part. Full RFC 5321 grammar (quoted strings, escaped chars, comments) is not worth the maintenance cost for a signup form.
- Internationalized addresses. Unicode domains and email addresses need IDN handling; a pure ASCII regex cannot represent them. Decide explicitly whether your product supports them.
Separate format checks from real verification
A regex proves the string looks like an address. It does not prove the mailbox exists, accepts mail, or belongs to anyone. For a signup form, pair a sensible format check with a confirmation email, and put a length limit on the server side (254 characters is the RFC ceiling for a full address). That combination filters typos without pretending to know what the mail server will do.
FAQ
Why does my pattern reject an address that my mail client accepts?
Mail clients often skip validation entirely and just send. Your pattern is stricter than the transport layer. Compare it against RFC 5322 examples and decide whether the rejected case is worth supporting.
Is there a single regex that covers every valid email?
No. The full grammar is too complex to maintain, and every published "perfect" regex either rejects valid edge cases or is a performance hazard. Ship a good-enough pattern and a confirmation step instead.
Should I validate on the client or the server?
Both, for different reasons. Client-side catches typos instantly; server-side is the boundary you can trust. Never rely on client-side checks alone.
Can I reuse this regex in Python and JavaScript?
The pattern itself is portable, but escaping differs slightly between flavors. Re-test the exact pattern in each engine before shipping.