What would be a globally accepted regular expression to match e

I have seen many examples, with many 'no, you missed something' comments. What is the right way to match an e-mail address?

For Sanity sake, only fully-qualified domain names, no @localhost allowed. (or, both ways)

Subdomains must be allowed (issac@deptartment.company.museum)


这个正则表达式符合RFC 2822中描述的语法,它很长,但RFC中描述的语法很复杂。


It is impossible to do so in a pure regex. Regexen cannot match nested parentheses, which the full RFC spec requires. (The latest RFC on this matter is RFC5322, only released a few months ago.)

Full validation of email addresses requires something along the lines of a CFG, and there are a few more things to be wary of; for example, email addresses can contain '' , the null character... so you can't use any of C's normal string functions on them.

I actually feel a bit weird answering a question with a link to something I've written, but as it so happens, I have one I prepared earlier: a short and (as far as I can tell) fully-compliant validator, in Haskell; you can see the source code here. I imagine the code could be easily ported to any similar parsing library (perhaps C++'s Boost.Spirit), or just as easily hooked into from another language (Haskell has a very simple way for C to use Haskell code, and everything can use C bindings...)

There are also extensive test cases in the source code (I didn't export them from the module), which are due to Dominic Sayers, who has his own version of an RFC-compliant parser in PHP. (Several of the tests fail, but they are more strict than RFC5322 specifies, so I'm fine with that at the moment.)


That was asked here a couple of weeks ago. What it comes down to is, there are many legal addresses that an easy regex won't match. It takes a truly insane regex to match the majority of legal addresses. And even then, a syntactically legal address doesn't guarantee the existence of an account behind it - take foo@example.invalid, for example.

链接地址: http://www.djcxy.com/p/16548.html

上一篇: 字符串操作与Regexps

下一篇: 什么是全球公认的正则表达式来匹配e