Live data from Hacker News

Ruby's Email Address Regexp

github.com

11–20 of 69 posts

Re: Ruby's Email Address Regexp

#11
post #9
post #3

The best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.

This depends on your use-case. If you're writing a mail agent, then you do probably want to parse email addresses in their entirety. If you're writing a website that accepts email addresses and wants to make sure the user doesn't just type "foo", then yeah, check for `.+@.+` and call it a day.

If you are writing a webpage that accepts email addresses, then please just use and check for:

    document.querySelector("[type='email']").validity.valid
This will not only give you email validation for free, but also gives users with software keyboards (e.g. on phones) a contextual keyboard.

Re: Ruby's Email Address Regexp

#13
post #3

The best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.

The regex listed there isn’t that much more complex. It’s basically a check for *@a*.* where * is some minimal whitelist of valid characters and a is an alphanumeric to start the domain name.

Re: Ruby's Email Address Regexp

#14
post #4
post #3

The best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.

There is really no point going further than this. It's more likely that someone will type the email wrong but still valid than they will type it completely invalid. There are also some completely wrong validators out there which expect the TLD to be 2-3 chars only. The ultimate email validation is just trying to send an email to the address and confirming with a code/link.

I have had people rewrite my .io to .com because they were sure it was wrong, or something like that.

Re: Ruby's Email Address Regexp

#16

I feel like every developer at some point Googles "URL regex" and is inevitably led down a rabbit hole of different regexes — some optimizing for maximum accuracy, others for minimum insanity. Having been down that rabbit hole before myself, I have to admit, this email regex is tamer than I expected it to be.

E-mails, URLs, file names/extensions, these are the bane of my RegEx existence. Agreed, this is not as bad as I've seen in other places.

Re: Ruby's Email Address Regexp

#17
post #12
post #7

Earlier quoted context omitted.

There is nothing before the first @, and nothing after the last @.

While that's true, `@@.@` fulfils "an @ symbol with something before it, and something after it".

The original comment probably meant something like this: ^[^@]@[^@]$

Re: Ruby's Email Address Regexp

#18
post #3

The best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.

It’s probably a waste of time for an individual developer to write a one-off complicated regex for a contact form. A team of contributors to a standard library should be optimizing regex a bit more since doing so will save so many developers time vs using even a very simple one-off regex, when testing is accounted. The optimizations here are reasonable and internationally compatible.

Re: Ruby's Email Address Regexp

#19
post #13
post #3

The best email regex just checks for an @ symbol with something before it, and something after it. Anything more complex is a waste of time.

The regex listed there isn’t that much more complex. It’s basically a check for *@a*.* where * is some minimal whitelist of valid characters and a is an alphanumeric to start the domain name.

And even though that sounds reasonable, it is still wrong. Technically, you do not even need the dot, if you add an MX record to a whole TLD or have some other funky DNS setup.

Source: https://www.netmeister.org/blog/email.html

Re: Ruby's Email Address Regexp

#20
There are basically three levels of address checking:

1) You need to validate an email field for login or a website - checking for an @ mark with some text before and at least one . after the @ will do for this.

2) You need to do some sort of address validation, library regexps like this will do for 99.9...% of these.

3) You are building an email handling system which needs to actually support the RFCs, in which case regexp will not handle what you need, and you need to use a proper parser, like https://github.com/mikel/mail/tree/master/lib/mail/parsers

Ref: I am the original author of the Ruby mail gem.

Post reply on HN