Live data from Hacker News

Ruby's Email Address Regexp

github.com

1–10 of 69 posts

Re: Ruby's Email Address Regexp

#2
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.

Re: Ruby's Email Address Regexp

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

Re: Ruby's Email Address Regexp

#6
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.

Never mind the regex, `email.indexOf("@") > 2` does the trick and faster if you happen to need to check many emails. All websites these days require verification of emails (regardless of whether or not it's necessary), and if that's not enough validation, I don't know what is!

Re: Ruby's Email Address Regexp

#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.
Post reply on HN