史上最复杂的验证邮件地址的正则表达式

用正则表达式验证邮件地址似乎是一件简单的事情,但是如果要完美的验证一个合规的邮件地址,其实也许很复杂。

史上最复杂的验证邮件地址的正则表达式

邮件地址的规范来自于 RFC 5322 。有一个网站 emailregex.com 专门列出各种编程语言下的验证邮件地址的正则表达式,其中很多正则表达式都是我听说过而从未见过的复杂——我想说,做这个网站的程序员是被邮件验证这件事伤害了多深啊!

其实,在产品环境中,一般来说并不需要这么复杂的正则表达式来做到99.99%正确。一般来说,从执行效率和测试覆盖率来说,只需要一个简单的版本即可:

  1. <span class="str">/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/</span><span class="pln">i</span>

那么下面我们来看看这些更严谨、更复杂的正则表达式吧:

 

验证邮件地址的通用正则表达式(符合 RFC 5322 标准)

  1. <span class="pun">(?:[</span><span class="pln">a</span><span class="pun">-</span><span class="pln">z0</span><span class="pun">-</span><span class="lit">9</span><span class="pun">!#</span><span class="pln">$</span><span class="pun">%&</span><span class="str">'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'</span><span class="pun">*+/=?^</span><span class="pln">_</span><span class="str">`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])</span>

由于各种语言对正则表达式的支持不同、语法差异和覆盖率不同,所以,不同语言里面的正则表达式也不同:

 

Python

这个是个简单的版本:

  1. <span class="pln">r</span><span class="str">"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)"</span>

 

Javascript

这个有点复杂了:

  1. <span class="str">/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/</span><span class="pln">i</span>

 

Swift

  1. <span class="pun">[</span><span class="pln">A</span><span class="pun">-</span><span class="pln">Z0</span><span class="pun">-</span><span class="lit">9a</span><span class="pun">-</span><span class="pln">z</span><span class="pun">.</span><span class="pln">_</span><span class="pun">%+-]+@[</span><span class="pln">A</span><span class="pun">-</span><span class="typ">Za</span><span class="pun">-</span><span class="pln">z0</span><span class="pun">-</span><span class="lit">9.</span><span class="pun">-]+</span><span class="pln">\\</span><span class="pun">.[</span><span class="pln">A</span><span class="pun">-</span><span class="typ">Za</span><span class="pun">-</span><span class="pln">z</span><span class="pun">]{</span><span class="lit">2</span><span class="pun">,</span><span class="lit">6</span><span class="pun">}</span>

 

PHP

PHP 的这个版本就更复杂了,覆盖率就更大一些:

  1. <span class="str">/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/</span><span class="pln">iD</span>

 

Perl / Ruby

对与 PHP 的版本,Perl 和 Ruby 表示不服,可以更严谨:

  1. <span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:(?:(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|"</span><span class="pun">(?:[^</span><span class="pln">\"\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.|(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">]))*</span><span class="str">"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="str">"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[\t]))*"</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*@(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*|(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|"</span><span class="pun">(?:[^</span><span class="pln">\"\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.|(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">]))*</span><span class="str">"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln">\t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln">\t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="str">"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|"</span><span class="pun">(?:[^</span><span class="pln">\"\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.|(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">]))*</span><span class="str">"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)|(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="str">"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)*:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:(?:(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|"</span><span class="pun">(?:[^</span><span class="pln">\"\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.|(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">]))*</span><span class="str">"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="str">"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*@(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*|(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|"</span><span class="pun">(?:[^</span><span class="pln">\"\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.|(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">]))*</span><span class="str">"(?:(?:\r\n)?[ \t])*)*\<(?:(?:\r\n)?[ \t])*(?:@(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="str">"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|"</span><span class="pun">(?:[^</span><span class="pln">\"\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.|(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">]))*</span><span class="str">"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\>(?:(?:\r\n)?[ \t])*)(?:,\s*(?:(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="str">"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|"</span><span class="pun">(?:[^</span><span class="pln">\"\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.|(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">]))*</span><span class="str">"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)(?:</span><span class="pln">\.</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="str">"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)*</span><span class="pln">\<</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:@(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*(?:,@(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln">\t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*)*:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*)?(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|"</span><span class="pun">(?:[^</span><span class="pln">\"\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.|(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">]))*</span><span class="str">"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="str">"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*@(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*(?:[^()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\Z|(?=[\["</span><span class="pun">()<>@,;:</span><span class="pln">\\</span><span class="str">".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\] \0</span><span class="lit">00</span><span class="pun">-</span><span class="pln">\0</span><span class="lit">31</span><span class="pun">]+(?:(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])+|</span><span class="pln">\Z</span><span class="pun">|(?=[</span><span class="pln">\[</span><span class="str">"()<>@,;:\\"</span><span class="pun">.</span><span class="pln">\[\]</span><span class="pun">]))|</span><span class="pln">\[</span><span class="pun">([^</span><span class="pln">\[\]\r\\</span><span class="pun">]|</span><span class="pln">\\</span><span class="pun">.)*</span><span class="pln">\]</span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*</span><span class="pln">\></span><span class="pun">(?:(?:</span><span class="pln">\r\n</span><span class="pun">)?[</span><span class="pln"> \t</span><span class="pun">])*))*)?;</span><span class="pln">\s</span>

 

Perl 5.10 及以后版本

上面的版本,嗯,我可以说是天书吗?反正我是没有解读的想法了。当然,新版本的 Perl 语言还有一个更易读的版本(你是说真的么?) 

  1. <span class="pun">/(?(</span><span class="pln">DEFINE</span><span class="pun">)</span>
  2. <span class="pun">(?<</span><span class="pln">address</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">mailbox</span><span class="pun">)</span><span class="pun">|</span><span class="pun">(?&</span><span class="kwd">group</span><span class="pun">))</span>
  3. <span class="pun">(?<</span><span class="pln">mailbox</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">name_addr</span><span class="pun">)</span><span class="pun">|</span><span class="pun">(?&</span><span class="pln">addr_spec</span><span class="pun">))</span>
  4. <span class="pun">(?<</span><span class="pln">name_addr</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">display_name</span><span class="pun">)?</span><span class="pun">(?&</span><span class="pln">angle_addr</span><span class="pun">))</span>
  5. <span class="pun">(?<</span><span class="pln">angle_addr</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">CFWS</span><span class="pun">)?</span><span class="pun"><</span><span class="pun">(?&</span><span class="pln">addr_spec</span><span class="pun">)</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">CFWS</span><span class="pun">)?)</span>
  6. <span class="pun">(?<</span><span class="kwd">group</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">display_name</span><span class="pun">)</span><span class="pun">:</span><span class="pun">(?:(?&</span><span class="pln">mailbox_list</span><span class="pun">)</span><span class="pun">|</span><span class="pun">(?&</span><span class="pln">CFWS</span><span class="pun">))?</span><span class="pun">;</span>
  7. <span class="pun">(?&</span><span class="pln">CFWS</span><span class="pun">)?)</span>
  8. <span class="pun">(?<</span><span class="pln">display_name</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">phrase</span><span class="pun">))</span>
  9. <span class="pun">(?<</span><span class="pln">mailbox_list</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">mailbox</span><span class="pun">)</span><span class="pun">(?:</span><span class="pun">,</span><span class="pun">(?&</span><span class="pln">mailbox</span><span class="pun">))*)</span>
  10. <span class="pun">(?<</span><span class="pln">addr_spec</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">local_part</span><span class="pun">)</span><span class="pln"> \@ </span><span class="pun">(?&</span><span class="pln">domain</span><span class="pun">))</span>
  11. <span class="pun">(?<</span><span class="pln">local_part</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">dot_atom</span><span class="pun">)</span><span class="pun">|</span><span class="pun">(?&</span><span class="pln">quoted_string</span><span class="pun">))</span>
  12. <span class="pun">(?<</span><span class="pln">domain</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">dot_atom</span><span class="pun">)</span><span class="pun">|</span><span class="pun">(?&</span><span class="pln">domain_literal</span><span class="pun">))</span>
  13. <span class="pun">(?<</span><span class="pln">domain_literal</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">CFWS</span><span class="pun">)?</span><span class="pln"> \[ </span><span class="pun">(?:</span><span class="pun">(?&</span><span class="pln">FWS</span><span class="pun">)?</span><span class="pun">(?&</span><span class="pln">dcontent</span><span class="pun">))*</span><span class="pun">(?&</span><span class="pln">FWS</span><span class="pun">)?</span>
  14. <span class="pln">\] </span><span class="pun">(?&</span><span class="pln">CFWS</span><span class="pun">)?)</span>
  15. <span class="pun">(?<</span><span class="pln">dcontent</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">dtext</span><span class="pun">)</span><span class="pun">|</span><span class="pun">(?&</span><span class="pln">quoted_pair</span><span class="pun">))</span>
  16. <span class="pun">(?<</span><span class="pln">dtext</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">NO_WS_CTL</span><span class="pun">)</span><span class="pun">|</span><span class="pun">[</span><span class="pln">\x</span><span class="lit">21</span><span class="pun">-</span><span class="pln">\x</span><span class="lit">5a</span><span class="pln">\x</span><span class="lit">5e</span><span class="pun">-</span><span class="pln">\x</span><span class="lit">7e</span><span class="pun">])</span>
  17. <span class="pun">(?<</span><span class="pln">atext</span><span class="pun">></span><span class="pun">(?&</span><span class="pln">ALPHA</span><span class="pun">)</span><span class="pun">|</span><span class="pun">(?&</span><span class="pln">DIGIT</span><span class="pun">)</span><span class="pun">|</span><span class="pun">[!#</span><span class="pln">\$</span><span class="pun">%&</span><span class="str">'*+-/=?^_`{|}~])</span>
  18. <span class="str">(?<atom> (?&CFWS)? (?&atext)+ (?&CFWS)?)</span>
  19. <span class="str">(?<dot_atom> (?&CFWS)? (?&dot_atom_text) (?&CFWS)?)</span>
  20. <span class="str">(?<dot_atom_text> (?&atext)+ (?: \. (?&atext)+)*)</span>
  21. <span class="str">(?<text> [\x01-\x09\x0b\x0c\x0e-\x7f])</span>
  22. <span class="str">(?<quoted_pair> \\ (?&text))</span>
  23. <span class="str">(?<qtext> (?&NO_WS_CTL) | [\x21\x23-\x5b\x5d-\x7e])</span>
  24. <span class="str">(?<qcontent> (?&qtext) | (?&quoted_pair))</span>
  25. <span class="str">(?<quoted_string> (?&CFWS)? (?&DQUOTE) (?:(?&FWS)? (?&qcontent))*</span>
  26. <span class="str">(?&FWS)? (?&DQUOTE) (?&CFWS)?)</span>
  27. <span class="str">(?<word> (?&atom) | (?&quoted_string))</span>
  28. <span class="str">(?<phrase> (?&word)+)</span>
  29. <span class="str"># Folding white space</span>
  30. <span class="str">(?<FWS> (?: (?&WSP)* (?&CRLF))? (?&WSP)+)</span>
  31. <span class="str">(?<ctext> (?&NO_WS_CTL) | [\x21-\x27\x2a-\x5b\x5d-\x7e])</span>
  32. <span class="str">(?<ccontent> (?&ctext) | (?&quoted_pair

相关推荐