6 ms·
Many hours spent here, trying to create the ultimate email regex, while telling my PMs and managers that there was no such thing.
by strictnein 10y ago
Many hours spent here, trying to create the ultimate email regex, while telling my PMs and managers that there was no such thing.
- jandrese 10y agoIn the back of the Camel book there is an email matching regex. It takes the entire page. It probably doesn't handle some Unicode edge cases properly. Validating emails with regexes is one of those things that seems like it should be fairly straightforward, but thanks to the language in the RFC it is actually a nightmare of rabbit holes filled with landmines.
- EvilTerran 10y agoThat would be the one shown here: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html I don't know about unicode, but I do note it says it doesn't handle comments (because, apparently, they can be nested - and perl's REs lack recursion).
- jandrese 10y ago> Implementing validation with regular expressions somewhat pushes the limits of what it is sensible to do with regular expressions (rest of the page is filled with line noise) LOL
- justinator 10y agoPerl has supported recursion in regex since around v5.10. The giant regex is not from the Camel book, but from O'Reilly's, Mastering Regular Expressions which does give the example in Perl, but was printed before v5.10 of Perl was released. (3rd ed. of Mastering Expressions was released in 2006; Perl v5.10 was released in 2006.). I believe the regex was published in the very first edition.
- EvilTerran 10y agoAh yes, sorry, it would have been more correct to say "perl's REs lacked recursion at the time ". I use PCRE pretty frequently, so knew that it's supported recursion for some time; but it's been a while since I've done anything non-trivial with perl itself, so couldn't remember if it too had that functionality or I was just getting their feature-sets confused. Evidently I guessed wrong - thanks for the correction.
- 77pt77 10y agoRFC 822 is the Internet's cancer. Really. Email addresses should have never been allowed to have comments. Since every new RFC maintains compatibility with this one, we'll probably be stuck with these poor choices for decades.
- Cyph0n 10y agoEmail addresses allow comments? Who the heck thought that was a smart idea!? I guess this is what you're referring to: James Doe <jdoe@a.com> The email - which is inside the <> - should be parsed correctly, while the rest should be treated as a "comment" [1]. [1]: https://www.cs.tut.fi/~jkorpela/rfc/822addr.html https://www.cs.tut.fi/~jkorpela/rfc/822addr.html
- 77pt77 10y agoNo No. Stuff like: * comments are allowed with parentheses at either end of the local-part; e.g. john.smith(comment)@example.com and (comment)john.smith@example.com are both equivalent to john.smith@example.com. [1] * Also have a look at quotes [1]https://en.wikipedia.org/wiki/Email_address#Local-part https://en.wikipedia.org/wiki/Email_address#Local-part
- xiaoma 10y agoAnd the irony is that regular expressions, by definition, can't recurse. Nobody's gonna rename them to turing expressions, though.
- captaincrowbar 10y agoBeen there, done that. There's only one way to check email addresses: /.+@.+/ Anything else is going to give you a false negative sooner or later. Yeah, you can spend hours framing a complicated expression that carefully tests for exactly what the current bucket of RFCs specify, but you're guaranteed to run into an MTA somewhere that disagrees with you, and you can argue until the cows come home that you're right and they're wrong, but it's still you that's going to have to change to accommodate them in the end.
- aroch 10y agoI would think you'd use: /.+@.+\..+/
- pmlnr 10y agonope. root@localhost is a valid mail address.
- aroch 10y agoHow often are you validating email addresses for localhost!? Those are pretty much never a valid email for >>>99% of applications
- mason55 10y agoRight, so if you're not going to accept the full RFC then where do you draw the line? Your arbitrary line will assuredly be different from someone else's.
- nothrabannosir 10y agoThat's the whole point of the original post: don't bother thinking about it, let the mta do that for you. The "@" is a courtesy to make sure people didn't mistakenly put their name in. Don't validate, just do a sanity check.
- 10y ago
- edblarney 10y agoThe issue is not regexing, it's lack of real standard in email format :)
- ComputerGuru 10y ago^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$ This and other email validation here: https://github.com/neosmart/web/blob/master/Web%20Toolkit/EmailFilter.cs https://github.com/neosmart/web/blob/master/Web%20Toolkit/Em...
- bduerst 10y agoIs that chinese character compatible? (half-serious)
- wodenokoto 10y agoWhat I don't understand is, don't programs like thunderbird or mutt have a module or function for email sanity check, and why hasn't that been ported as a module for every language imaginable?
- tonyedgecombe 10y agoBeing the maintainer of that would be the worst programming job possible.
- foota 10y agoI'm going to have to go ahead and agree with a comment in the javax internet address code here, if you insist on checking against the rfc, you should really just give up and use a lexer.
- fasinfranco 10y agoThis one covers like 99.9% of the cases: [\w]\S+@\S+[a-z]+
- manarth 10y agoThe pedant in me wants to point out that the TLD can be uppercase, as the domain part is case-insensitive. In any case, it probably won't be too long before we get emoji TLDs, or UTF8 flags to replace the country-code :-D