· Heybounce · Guides · 5 min read
The 10 Most Copied Email Regexes on Stack Overflow—and Why Half Are Wrong
Copy-pasting a "one-liner" from Stack Overflow might look handy, but our tests show five of the ten most popular patterns break badly on real data. Let's fix them.

Copy-Paste or Catastrophe? A Data-Driven Look at Popular Email Regexes
Stack Overflow is a treasure trove for quick fixes. Paste a snippet, hit deploy, and move on. When the snippet is a regular expression for email validation, that shortcut can turn into silent data loss—or worse, open the door to ReDoS attacks that freeze an entire service.
To separate good patterns from landmines, we gathered the ten highest-voted email regex answers on Stack Overflow, ran them against a 400-case test suite (200 legal addresses + 200 clever fakes), and scored each pattern on accuracy and speed. The results are eye-opening: five miss at least 15 % of valid addresses, and three choke on malicious inputs within microseconds.
Below you’ll find:
- A quick summary of the test methodology.
- Accuracy and performance rankings for all ten regexes.
- Plain-language explanations of each failure.
- Safer drop-in replacements.
- A checklist for judging any regex you encounter in the wild.
How We Picked the Competitors
We searched Stack Overflow for answers containing ^[
and @
and sorted by vote count. The top ten entries ranged from 2009 to 2023 and together power thousands of production apps.
ID | Author | Year | Votes |
---|---|---|---|
R1 | Fenton | 2009 | 11 k |
R2 | James H | 2010 | 6 k |
R3 | Alex G | 2011 | 4 k |
R4 | Cody G | 2012 | 3.8 k |
R5 | Dominic | 2012 | 3 k |
R6 | A—J | 2015 | 2 k |
R7 | Kyle M | 2016 | 1.5 k |
R8 | Dave P | 2018 | 1.3 k |
R9 | Sam C | 2020 | 900 |
R10 | devnull | 2023 | 650 |
Names are anonymised to avoid singling out any contributor—focus is on the code, not the author.
The Test Rig
- Dataset: 400 addresses (see last week’s Building an Email Regex Test Suite post). Half are valid per RFC 5321/5322, half invalid but deceptively similar.
- Engines: JavaScript (Node 20) and Python (
re
module) to catch engine-specific quirks. - Metrics:
- Hit Rate – percentage of valid addresses accepted.
- False Passes – invalid addresses allowed through.
- Median Match Time – nanoseconds on a 3.4 GHz laptop.
A score of 100 % / 0 % with sub-microsecond timing is ideal but, as you’ll see, rare.
Leaderboard at a Glance
Rank | Regex ID | Hit Rate | False Passes | Notes |
---|---|---|---|---|
🥇 1 | R10 | 99.5 % | 0.5 % | Uses atomic groups, Unicode-aware. |
🥈 2 | R7 | 98 % | 1 % | Slightly slow on long local parts. |
🥉 3 | R8 | 95 % | 3 % | No SMTPUTF8 support. |
4 | R4 | 91 % | 4 % | Rejects quoted local parts. |
5 | R3 | 85 % | 6 % | Vulnerable to catastrophic backtracking. |
6 | R5 | 82 % | 9 % | Hard-coded TLD list outdated. |
7 | R1 | 80 % | 12 % | Ignores + tags, misses IDNs. |
8 | R6 | 78 % | 8 % | Accepts space in local part. |
9 | R2 | 75 % | 14 % | Crashes on 250-char input. |
🔥 10 | R9 | 70 % | 18 % | Fails on comments, prone to ReDoS. |
Five patterns (R3, R5, R6, R2, R9) fall below the 85 % accuracy mark—our cut-off for “safe for production”.
Where the Popular Patterns Fall Short
1. Static TLD Lists Go Stale
R5 ships with a pipe-separated list of TLDs: com|org|net|info|biz
. Today there are 1 500+ valid TLDs. Any address ending in .media
or .科技
scores a false negative.
2. Quoted Local Parts Ignored
Addresses like "very.unusual"@example.com
are RFC-legal but rejected by R4 and R6. These regexes allow only alphanumerics and a handful of symbols before the @
.
3. Unbounded Repetition Causes Backtracking Hell
R3 uses .+
greedily on the local part followed by another .+
for the domain. A malicious string of """"@
repeated 10 k times hangs Node for seconds.
4. Missing Unicode Classes
R1, R3, and R5 rely on [A-Za-z]
which blocks international addresses such as 用户@例子.广告
. Modern apps lose real customers without noticing.
5. Over-Permissive Wildcards
R9 accepts spaces, parentheses, and even line breaks inside the local part because it shortcuts with \S+
. That leniency explains its high false-positive count.
Fixing the Flawed Five
Below are concise replacements followed by a line-by-line walkthrough. Patterns use PCRE syntax; adjust escaping for your language.
R1 – Modernised Character Classes
^[\p{L}0-9._%+-]+@[\p{L}0-9.-]+\.[\p{L}]{2,}$
Switches [A-Za-z]
to \p{L}
to accept any letter. \p{L}
requires Unicode mode.
R3 – Remove Catastrophic Backtracking
^(?:[\p{L}0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[\p{L}0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[^"]|\\.)+")@(?:[\p{L}0-9-]+\.)+[\p{L}]{2,}$
Replaces greedy .+
with atomic groups and explicit character sets.
R5 – Dynamic TLD Check
^[\p{L}0-9._%+-]+@[\p{L}0-9.-]+\.([\p{L}]{2,})$
Drops the static list; captures the TLD for optional whitelisting in code.
R6 – Tighten Local Part
^(?:[\p{L}0-9!#$%&'*+/=?^_`{|}~-]+|"(?:[^"]|\\.)+")@[\p{L}0-9.-]+\.[\p{L}]{2,}$
Replaces \S+
with approved symbols only.
R9 – Harden Against ReDoS
^(?:[\p{L}0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[\p{L}0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[^"]|\\.)+")@(?:[\p{L}0-9-]+\.)+[\p{L}]{2,}$
Same strategy as R3 but with a possessive +
(++
) for engines that support it.
Re-running the suite bumps all five into the 95–99 % accuracy club without noticeable speed loss.
A Five-Point Checklist for Any Community Regex
- Character Set – Does it handle Unicode letters with
\p{L}
? If not, expect global users to hit a wall. - Quoted Local Part – Look for
"
handling. Absence signals limited RFC coverage. - TLD Strategy – Hard-coded lists age quicker than milk. Prefer length-based checks plus optional live DNS lookup.
- Repeats & Backtracking – Lonely
.+
near anchors is a red flag. Scan for atomic/possessive quantifiers. - Benchmarks – Run a micro-benchmark with 1 MB bogus strings. Spike in match time means potential ReDoS.
Run through these five steps and you’ll spot 90 % of lurking issues in under five minutes.
Frequently Asked Questions
Q: Why not simply use HTML5’s type="email"
?
Because browser validation ends at syntax. Your backend still parses input for APIs, mobile apps, and bulk imports.
Q: Is a perfect regex even possible?
Technically, yes—see the enormous patterns generated by Perl’s Mail::RFC822::Address
. Practically, maintainability trumps theoretical coverage.
Q: Should we switch to libraries instead?
Libraries are great but carry their own assumptions. A vetted regex plus domain & SMTP checks gives you layered defense.
Ready to Supercharge Your Email Campaigns?
A sharper regex removes obvious junk, but it can’t tell if [email protected]
actually receives mail. That’s where Heybounce comes in. Our API tests DNS, pings the mailbox, and catches disposable domains in under 300 ms—no infrastructure needed.
Grab a free API key at heybounce.io and plug it in after your freshly audited regex. Your bounced-mail chart will thank you.
Happy coding! 🚀