Live data from Hacker News

Email Regex that works 99.99%

emailregex.com

41–50 of 65 posts

Re: Email Regex that works 99.99%

#41
post #11

Alternatively, don't validate email addresses with a regex because it's pointless. Check that at least an '@' symbol is present and just send a damn email already[0]. Or better, don't send a damn email at all and let them access it because it's pointless to have an extra verification step. If you really want to do some clientside validation, just keep a basic regex and warn the user if the address doesn't seem right.…

> Or better, don't send a damn email at all and let them access it because it's pointless to have an extra verification step

I've heard lots of people say not to bother validating but I always thought it was because you could just confirm with a verification email. Why's this step useless?

Re: Email Regex that works 99.99%

#42

Yeah this is not great practice. We built an app where sending a validation email upfront was not a practical option some time ago, and the best strategy I found for ensuring the email was valid was to lookup the MX records and ask the mailserver for the given domain, by issuing RCPT commands. Many mailservers will just drop connections when RCPT is for someone who doesn't exist or can't be routed to which was a good…

Did you remember to fall back on a A and AAAA lookup if there is no MX record? That is what you are supposed to do.

Re: Email Regex that works 99.99%

#43
post #6

I believe this falls under the category of "things that may be fun to play around with but should never be used in a real system". Unfortunately, I bet there are thousands of "real systems" employing regexes like this... How many problems does this solve? Probably zero. How many does (/will) it cause? Probably much more than zero.

Not sure why this has been downvoted, but yes these sort of things do cause more problems than they solve. Case in point, at work the other day I found a bug in a service I manage. It consists of a front end form (built by one team), which submits data to another system (built by my team), which then passes the data to a third party. The third party was rejecting the data we were trying to send them as the email addr…

So what caused the problem, wasn't that front end and 3rd party was checking if email is valid, but the fact that there is no standard way of checking it.

I'm not saying that checking email address with regex is good way of doing it, but there is countless examples of people doing it and it would help a lot if everyone would use one standardized regex for that.

Re: Email Regex that works 99.99%

#44
Meh. I wish people would just give up on trying to validate email adresses all together (except for maybe basic stuff like checking for an @). They'll almost always forget about some edge case.

I use a pretty ununsual TLD (.su, the old TLD for the Soviet Union, which still remains in the root zone), and from time to time, I come across a site that won't accept my email address because of that. Most of those sites turn out to be generally crappy though, so not much of a loss…

Also, many sites don't accept + in email adresses, which is annoying as hell if you want to use the address extension feature of Postfix et al.

Re: Email Regex that works 99.99%

#45
post #41
post #11

Alternatively, don't validate email addresses with a regex because it's pointless. Check that at least an '@' symbol is present and just send a damn email already[0]. Or better, don't send a damn email at all and let them access it because it's pointless to have an extra verification step. If you really want to do some clientside validation, just keep a basic regex and warn the user if the address doesn't seem right.…

> Or better, don't send a damn email at all and let them access it because it's pointless to have an extra verification step I've heard lots of people say not to bother validating but I always thought it was because you could just confirm with a verification email. Why's this step useless?

I don't get it either. Not doing any verification ist just an invitation for trouble. "My password doesn't work, why can't you send me a new one?"

Re: Email Regex that works 99.99%

#47
Jeez, that's 100 email addresses in a million that it won't work on. Plus it's a pain in the butt. [Edit: Though I suspect that 99.99% figure was made up]

Just get people to enter their email twice (which filters out most mistakes where people are entering their names or somesuch), don't validate it with regex, during the signup process make sure you tell them to expect an email which they must confirm before they are added / before their account is activated. Send a confirmation email with a clickable link. If people don't get it, and the service is important to them, they'll try again or contact you through another means.

(I was involved with the running of a mailing list with well over 1m double-opt-in subscribers. Less than 100 of these turned out to be invalid [Edit: yeah, that's a guess, like the OP's 99.99%], and we dealt with it easily at our end, by properly handling any bounces)

Re: Email Regex that works 99.99%

#48

After extensive research, I have finally come up with a way to improve upon 99.99%. I have come up with a regex which will work for 100% of email addresses, and a significant number of regex engines, as an added bonus! Behold: .*@.* /s

  $ nc localhost 25
  220 uhura.z ESMTP
  MAIL FROM:me
  250 2.1.0 Ok
  RCPT TO:root
  250 2.1.5 Ok
  DATA
  354 End data with .
  Look ma, no @!
  .
  250 2.0.0 Ok: queued as 8DB653E0065
  quit
  221 2.0.0 Bye

Re: Email Regex that works 99.99%

#49

Note that these regexes aren't even matching the same thing, so who knows what these things are matching. Whatever it is, it's probably not 99.99% of the world's emails, and either way nobody is going to check that. Especially nobody is going to parse that Perl beast. As a tangential anecdote, I always thought it would be interesting to drop a backdoor into some canonical piece of code like this that noobs are bound…

There are two Perl regexes. The beast is for 5.8 (check your Perl version; it's probably above 5.12). The other is basically a BNF grammar and is trivial to parse. The only easier ones are those that throw up their hands and just look for an @ with characters before and after.

Re: Email Regex that works 99.99%

#50

Earlier quoted context omitted.

Completely useful, as part of a two step process:1 1. Filter with the regex - what's left has a valid format, making step 2 much saner. 2. Extract and validate the domain name - super simple now, because the domain component is known to be sane. (Optional but good idea 3: Handle exceptions....) Step 1 is almost always the hardest part, now it's mostly done.

The assumption here is "0. create a regex that does not have false negatives". You seem to be taking the author's word for the "99.9% Works" part - based on what? (Exactly.)

Correctness and utility are two different things, feel free to investigate the former whilst we discuss the latter....
Post reply on HN