Live data from Hacker News

Email Regex that works 99.99%

emailregex.com

31–40 of 65 posts

Re: Email Regex that works 99.99%

#31
Does't work on the dreaded "I am a terrible but nonetheless valid email address"@example.com.

I echo the advice of everyone else - validate with something very simple like .+@.+ and then by sending an email. Trying to recapture the complexity of the email system via a tool like regular expressions is tilting at windmills. It's like trying to develop a regular expression to determine whether a name is real or not.

https://www.exratione.com/2012/09/what-constitutes-an-accept...

Re: Email Regex that works 99.99%

#34

Earlier quoted context omitted.

.+@.+ Should do the trick

I considered that one, but I'm sure there's some system out there which doesn't require an account name ahead of the "@" symbol. We're shooting for 100% accuracy, after all. ;)

In that case, let me improve it further :-)

    /@./
Though I'm not really sure there could be an email without username part.

Re: Email Regex that works 99.99%

#36

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

Your verbosity is outlandish.

.*

will match all email addresses, and as an additional feature, all other strings too!

As we all know, more functionality with less code is better, therefore, this is clearly superior to all other regexes.

Re: Email Regex that works 99.99%

#37
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.…

> Check that at least an '@' symbol is present and just send a damn email already[0].

Checking there's a dot after the @ is probably a good idea. Although it theoretically blocks people who want mail sent to a TLD, that probably isn't a big effective issue.

Re: Email Regex that works 99.99%

#38
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, if you want to do something inbetween the two, and be reasonably certain it's a real email but not having to have the customer validate, do a quick MX lookup on the domain they supply (cache these on a local bind instance or whatever if you like), and if you really want to go a step further, send a quick HELO and headerset to the remote SMTP server (many libraries will do this for you), and see if responds with a 250.

Of course, all this takes time, so you always have a tradeoff between speed and validation level, from full-on user-validated email to "enter whatever".

Re: Email Regex that works 99.99%

#40
These have wildly different behaviour. The .NET and Javascript ones are even exchangeable (both are valid js). They will also not match internationalized domain names unless converted to punycode before validation.

IMO you either implement the RFC, or use the absolute dumbest validation possible: 1) there is one '@' character present 2) there is at least one dot on the right side. Anything else will exclude some valid addresses, and you're unlikely to ever hear feedback/complaints from someone who had their sign-up email rejected.

Post reply on HN