News for hackers. Stuff that matters.
Re:Perfect email regex finally found by andrewvc on Friday May 28, 2010 @04:10PM (#1387502)
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.
Re:Perfect email regex finally found by deutronium on Friday May 28, 2010 @04:19PM (#1387522)
One nice looking trick I've seen is checking the domain of the email address for an MX record. (e.g. http://php.dzone.com/news/php-email-validator-email-mx-d)

Possibly this approach is too slow to use by itself without a regex. Also maybe there are other problems with this method I'm not aware of?

Re:Perfect email regex finally found by pornel on Friday May 28, 2010 @04:26PM (#1387547)
MX record is not required. You should allow hosts with only A records too.

http://tools.ietf.org/html/rfc5321#section-5.1

Re:Perfect email regex finally found by curio on Friday May 28, 2010 @07:38PM (#1387979)
practically speaking, most respectable email services will have an MX record. i tested it on about 30,000 emails & a simple MX lookup caught about 40 gmial's and only 1 false positive.
Re:Perfect email regex finally found by drivebyacct on Friday May 28, 2010 @04:22PM (#1387532)
I suggested that a while back but was told that due to spam, you can get lots of false negatives from this technique and others.

The best way to validate an email remains to be a test email apparently.

Re:Perfect email regex finally found by spicyj on Friday May 28, 2010 @04:33PM (#1387577)
You must be thinking of something like this: http://www.webdigi.co.uk/blog/2009/how-to-check-if-an-email-...

Merely looking up MX records is something you do for a _domain_ and it's no different than the DNS requests your computer makes when you go to a website.

Re:Perfect email regex finally found by pkulak on Friday May 28, 2010 @04:26PM (#1387548)
Yeah, that's a really quick way to get IP banned from most email hosts.
Re:Perfect email regex finally found by alnayyir on Friday May 28, 2010 @04:17PM (#1387515)
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. Otherwise, it's write-once, read-never.

Re:Perfect email regex finally found by JadeNB on Friday May 28, 2010 @05:37PM (#1387741)
> 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 by huherto on Friday May 28, 2010 @06:33PM (#1387864)
> Given this, why would we use regular expressions? If I can get a way with a simple regexp I will use it. If it starts getting complicated I would use something else.(e.g. top down parser or a combination)
Re:Perfect email regex finally found by JadeNB on Friday May 28, 2010 @07:24PM (#1387954)
> If I can get a way with a simple regexp I will use it. If it starts getting complicated I would use something else.(e.g. top down parser or a combination)

Isn't that what I said?

Re:Perfect email regex finally found by huherto on Thursday June 03, 2010 @11:54AM (#1401648)
(If understood correctly) You based it in speed and memory efficiency. I rather base it on having something simple and readable.
Re:Perfect email regex finally found by adamc on Friday May 28, 2010 @04:21PM (#1387530)
That's just silly. Depending on what's in the string, writing a parser might be much better than a regex. Lots of parser libraries already out there, too.
Re:Perfect email regex finally found by Ixiaus on Friday May 28, 2010 @04:37PM (#1387589)
I can agree with this sentiment, there's also nothing wrong with writing a parser that breaks the input up into pieces and validates those individual parts using specialized regular expressions.
Re:Perfect email regex finally found by aphyr on Friday May 28, 2010 @04:52PM (#1387625)
Especially when the spec itself is in EBNF.
Re:Perfect email regex finally found by _delirium on Friday May 28, 2010 @04:56PM (#1387636)
True, though it's EBNF with a bunch of explanatory text and annotations.

What would be interesting, but I can't find with some googling: Has someone implemented a parser-generator based on the spec? The ideal would be that the parser specification looks a lot like the RFC, since then you'd have more confidence it was actually correct (and it'd be easier to maintain for future changes).

Re:Perfect email regex finally found by twopoint718 on Friday May 28, 2010 @07:28PM (#1387960)
There was a long post on the topic of 'regex-vs-parser' for email on reddit a while back. I hope I'm pointing to the correct person, but he wrote a parser in Haskell that validates against RFC5322: http://hackage.haskell.org/package/email-validate
Re:Perfect email regex finally found by aphyr on Friday May 28, 2010 @05:09PM (#1387666)
My comment above is a regex derived straightforwardly from the spec. Not a full parser, though.
Re:Perfect email regex finally found by alnayyir on Friday May 28, 2010 @07:15PM (#1387941)
By all means, use a CFG or a PEG for parsing emails.

While you do that, I'll do something that doesn't involve tweezers and code.

Re:Perfect email regex finally found by compay on Friday May 28, 2010 @11:30PM (#1388297)
Roberto Ierusalimschy of Lua fame often uses this example of how PEGs (parsing expression grammars) can be much easier than regexps for some tasks.

http://lua-users.org/lists/lua-l/2009-10/msg00817.html

Re:Perfect email regex finally found by huherto on Friday May 28, 2010 @06:22PM (#1387839)
If I remember correctly my theory of languages course a parser (implemented in a turing machine) is more powerful than a reggex (which is a finite state machine).
Re:Perfect email regex finally found by FlorinAndrei on Friday May 28, 2010 @04:18PM (#1387520)
Compared to regex, by definition, nothing can be uglier.