Home >Blog

Write a regex that matches an email address but rejects the obviously

Published Updated

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.com and example..com now fail.
  • The + after the first label requires at least one more label, so a bare TLD like user@localhost fails.

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

Illustration for Write a regex that matches an email addr
InputShould it pass?Why
[email protected]PassPlus addressing is valid and common in filters.
[email protected]PassMultiple domain labels are fine.
plainaddressFailNo @ at all.
user@@example.comFailDouble @ in the local part.
user [email protected]FailWhitespace in the local part.
user@exampleFailNo dot in the domain.
[email protected]FailLabel starts with a hyphen.
[email protected]FailEmpty 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.

Table of Contents

Information

  • Hits238
  • Published date2026/10/22

More Posts

Explore more articles from this section
Regex for IP Addresses: Accurate IPv4 and IPv6 Validation Examples
Sep 09, 2026811views

Regex for IP Addresses: Accurate IPv4 and IPv6 Validation Examples

Use regex to find possible IP addresses, then use the right validation rule for the job. This guide covers loo...

#regex for IP address#IP address regex#IPv4 regex#IPv6 regex
Password Validation Regex: Examples for Length, Numbers, Symbols, and Strong Passwords
Sep 07, 2026798views

Password Validation Regex: Examples for Length, Numbers, Symbols, and Strong Passwords

Learn practical password validation regex patterns for length, digits, symbols, and mixed character classes. T...

#password validation regex#regex for password validation#strong password regex#regex lookahead
How to Validate a URL with Regex: HTTP, HTTPS, Domains, and Query Strings
Sep 03, 2026714views

How to Validate a URL with Regex: HTTP, HTTPS, Domains, and Query Strings

Build a practical HTTP and HTTPS URL regex by separating schemes, DNS domains, ports, paths, query strings, an...

#URL validation regex#regex for URL validation#HTTP URL regex#HTTPS URL regex
How to Extract Email Addresses from Text with Regex in Python and JavaScript
Sep 15, 2026642views

How to Extract Email Addresses from Text with Regex in Python and JavaScript

Extract email addresses from any text with regex in Python and JavaScript: the pattern explained piece by piec...

#regex extract email#email regex python#email regex javascript#extract emails from text
How to Replace Text with Python Regex: re.sub() Examples and Common Mistakes
Sep 18, 2026640views

How to Replace Text with Python Regex: re.sub() Examples and Common Mistakes

Learn how to replace text safely with Python re.sub(), including capture groups, callback functions, case-inse...

#python regex replace#re.sub#regex capture groups#regex callback
What Regex Should You Use for Phone Numbers? Patterns for US and International Formats
Aug 28, 2026568views

What Regex Should You Use for Phone Numbers? Patterns for US and International Formats

Compare practical regex patterns for US formatted numbers, normalized local numbers, plus-prefixed internation...

#phone number regex#regex for phone number#US phone regex#international phone regex
Feedback email