Live data from Hacker News

Perfect email regex finally found

fightingforalostcause.net

41–50 of 118 posts

Re: Perfect email regex finally found

#41
post #28
post #14

I've accepted that it's best to treat people like grown-ups and if there's '@' and '.' and it's retyped then it passes. Someone can easily submit a fake name or phone number or street address, and e-mail's no different. If they get it wrong, intentionally or not, then they don't get their receipt, confirmation, validation link, etc. and I believe in most cases the incentive is there for them to get it right. In the r…

Along those lines, I've settled on the following overly permissive regex: /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/ -- it makes sure it looks something like an email address (a@b.cd)

What if I want to receive mail directly at my TLD?

Re: Perfect email regex finally found

#42
post #37
post #14

I've accepted that it's best to treat people like grown-ups and if there's '@' and '.' and it's retyped then it passes. Someone can easily submit a fake name or phone number or street address, and e-mail's no different. If they get it wrong, intentionally or not, then they don't get their receipt, confirmation, validation link, etc. and I believe in most cases the incentive is there for them to get it right. In the r…

Retyped!? Grown-ups can read what they write. Retyping only makes sense for password field, which is obfuscated and doesn't allow copy&paste.

I would estimate about 0.25% of people will make a typo like "@homail.com" or "@gmial.com"

Multiply that by say, 130,000 people, and you are dealing with 325 people who don't receive their download, etc. and are not happy!

I think what would be really awesome is a regex that catches these common typos and warns the user immediately.

Re: Perfect email regex finally found

#43
post #42
post #37

Earlier quoted context omitted.

Retyped!? Grown-ups can read what they write. Retyping only makes sense for password field, which is obfuscated and doesn't allow copy&paste.

I would estimate about 0.25% of people will make a typo like "@homail.com" or "@gmial.com" Multiply that by say, 130,000 people, and you are dealing with 325 people who don't receive their download, etc. and are not happy! I think what would be really awesome is a regex that catches these common typos and warns the user immediately.

You'd need something more along the lines of a spell checking algorithm to do that sanely. Don't abuse regular expressions for things they're not good at.

Re: Perfect email regex finally found

#44
post #14

I've accepted that it's best to treat people like grown-ups and if there's '@' and '.' and it's retyped then it passes. Someone can easily submit a fake name or phone number or street address, and e-mail's no different. If they get it wrong, intentionally or not, then they don't get their receipt, confirmation, validation link, etc. and I believe in most cases the incentive is there for them to get it right. In the r…

Your recommendation is almost as wrong as the regexp in the article, as both will reject the perfectly valid postmaster@ai address (check it out, "dig -t mx ai" will return a result, so that address must exist). The only thing you can be sure about in a non-local email address is that it contains at least one @ sign. More are allowed (for source routing), but hopefully no server is still configured to honor that, so you might get away with requiring exactly one @. Everything else is evil.

Oh, and don't forget to make sure that one component in your spam^W email processing chain correctly encodes unicode charaters in the domain part into punycode.

Re: Perfect email regex finally found

#46
post #3
post #2

How ugly do non-regex based email validation functions look? I've never seen one, but I've always wondered if that was a more elegant solution.

Regex is the only real sensible way to validate strings until something better is found. Even if you just wrote code to do it manually, you'd really just be writing a verbose and poorly implemented finite state machine that globbed symbols together, which in the end, would just be inferior to writing a well tested Regex string. Regex can be easier to read if you have something do a graphical expansion for you. Otherw…

> Regex is the only real sensible way to validate strings until something better is found.

There are plenty of ‘better’ (in the sense of ‘more powerful’) string-validation techniques. For example, lots of grammars are expressed in BNF; the languages that can be so expressed are (if I remember my Chomsky hierarchy correctly) the context-free grammars, a strictly larger class than the regular languages. The extra power comes from the fact that they have the expressive power of a finite-state automaton augmented by an (infinite) stack. (It's fair to argue that it's not ‘really’ infinite, since a computer's memory is finite; but, in that sense, real-life computers will never be Turing complete.)

(Of course, common ‘regular-expression’ libraries aren't actually regular any more, because of added features like capture groups. I don't know if they recognise all CFG's, though; I suspect not.)

Given this, why would we use regular expressions? Well, by intentionally sacrificing power, we can achieve faster matching (http://swtch.com/~rsc/regexp/regexp1.html) and, probably, lower memory usage. Sometimes this trade-off is worth it, even if it means that the match must be somewhat fuzzy; but sometimes one needs a precise match, and regexes just aren't up to the job.

Re: Perfect email regex finally found

#49
post #14

I've accepted that it's best to treat people like grown-ups and if there's '@' and '.' and it's retyped then it passes. Someone can easily submit a fake name or phone number or street address, and e-mail's no different. If they get it wrong, intentionally or not, then they don't get their receipt, confirmation, validation link, etc. and I believe in most cases the incentive is there for them to get it right. In the r…

My thoughts exactly. There are so many websites that tell me my valid email address is invalid that it's not even funny. These people then have to deal with phonecalls and lost business, because their form won't even submit without a valid email address (and why should I change if they're the ones that suck). BTW, the email address that doesn't work is "jon-whatever@jrock.us". The .us confuses people and the - confus…

I've seen code written in India that disallowed any email address containing one or more digits. This was on a site accepting email addresses from the general public, as well.
Post reply on HN