天天看點

史上最複雜的驗證郵件位址的正規表達式

用正規表達式驗證郵件位址似乎是一件簡單的事情,但是如果要完美的驗證一個合規的郵件位址,其實也許很複雜。

史上最複雜的驗證郵件位址的正規表達式

其實,在産品環境中,一般來說并不需要這麼複雜的正規表達式來做到99.99%正确。一般來說,從執行效率和測試覆寫率來說,隻需要一個簡單的版本即可:

<code>/^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i</code>

那麼下面我們來看看這些更嚴謹、更複雜的正規表達式吧:

<a target="_blank"></a>

<code>(?:[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&amp;'*+/=?^_`{|}~-]+)*|"(?:[\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])+)\])</code>

由于各種語言對正規表達式的支援不同、文法差異和覆寫率不同,是以,不同語言裡面的正規表達式也不同:

這個是個簡單的版本:

<code>r"(^[a-za-z0-9_.+-]+@[a-za-z0-9-]+\.[a-za-z0-9-.]+$)"</code>

這個有點複雜了:

<code>/^[-a-z0-9~!$%^&amp;*_=+}{\'?]+(\.[-a-z0-9~!$%^&amp;*_=+}{\'?]+)*@([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})?$/i</code>

<code>[a-z0-9a-z._%+-]+@[a-za-z0-9.-]+\\.[a-za-z]{2,6}</code>

php 的這個版本就更複雜了,覆寫率就更大一些:

<code>/^(?!(?:(?:\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}))\]))$/id</code>

對與 php 的版本,perl 和 ruby 表示不服,可以更嚴謹:

<code>(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[\t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\&lt;(?:(?:\r\n)?[ \t])*(?:@(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[\t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[\t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\&gt;(?:(?:\r\n)?[ \t])*)|(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*:(?:(?:\r\n)?[ \t])*(?:(?:(?:[^()&lt;&gt;@,;:\\".\[\]\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\&lt;(?:(?:\r\n)?[ \t])*(?:@(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\]\000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\&gt;(?:(?:\r\n)?[ \t])*)(?:,\s*(?:(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*|(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)*\&lt;(?:(?:\r\n)?[ \t])*(?:@(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*(?:,@(?:(?:\r\n)?[\t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*)*:(?:(?:\r\n)?[ \t])*)?(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|"(?:[^\"\r\\]|\\.|(?:(?:\r\n)?[ \t]))*"(?:(?:\r\n)?[ \t])*))*@(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*)(?:\.(?:(?:\r\n)?[ \t])*(?:[^()&lt;&gt;@,;:\\".\[\] \000-\031]+(?:(?:(?:\r\n)?[ \t])+|\z|(?=[\["()&lt;&gt;@,;:\\".\[\]]))|\[([^\[\]\r\\]|\\.)*\](?:(?:\r\n)?[ \t])*))*\&gt;(?:(?:\r\n)?[ \t])*))*)?;\s</code>

上面的版本,嗯,我可以說是天書嗎?反正我是沒有解讀的想法了。當然,新版本的 perl 語言還有一個更易讀的版本(你是說真的麼?) 

<code>/(?(define)</code>

<code>(?&lt;address&gt; (?&amp;mailbox) | (?&amp;group))</code>

<code>(?&lt;mailbox&gt; (?&amp;name_addr) | (?&amp;addr_spec))</code>

<code>(?&lt;name_addr&gt; (?&amp;display_name)? (?&amp;angle_addr))</code>

<code>(?&lt;angle_addr&gt; (?&amp;cfws)? &lt; (?&amp;addr_spec) &gt; (?&amp;cfws)?)</code>

<code>(?&lt;group&gt; (?&amp;display_name) : (?:(?&amp;mailbox_list) | (?&amp;cfws))? ;</code>

<code>(?&amp;cfws)?)</code>

<code>(?&lt;display_name&gt; (?&amp;phrase))</code>

<code>(?&lt;mailbox_list&gt; (?&amp;mailbox) (?: , (?&amp;mailbox))*)</code>

<code></code>

<code>(?&lt;addr_spec&gt; (?&amp;local_part) \@ (?&amp;domain))</code>

<code>(?&lt;local_part&gt; (?&amp;dot_atom) | (?&amp;quoted_string))</code>

<code>(?&lt;domain&gt; (?&amp;dot_atom) | (?&amp;domain_literal))</code>

<code>(?&lt;domain_literal&gt; (?&amp;cfws)? \[ (?: (?&amp;fws)? (?&amp;dcontent))* (?&amp;fws)?</code>

<code>\] (?&amp;cfws)?)</code>

<code>(?&lt;dcontent&gt; (?&amp;dtext) | (?&amp;quoted_pair))</code>

<code>(?&lt;dtext&gt; (?&amp;no_ws_ctl) | [\x21-\x5a\x5e-\x7e])</code>

<code>(?&lt;atext&gt; (?&amp;alpha) | (?&amp;digit) | [!#\$%&amp;'*+-/=?^_`{|}~])</code>

<code>(?&lt;atom&gt; (?&amp;cfws)? (?&amp;atext)+ (?&amp;cfws)?)</code>

<code>(?&lt;dot_atom&gt; (?&amp;cfws)? (?&amp;dot_atom_text) (?&amp;cfws)?)</code>

<code>(?&lt;dot_atom_text&gt; (?&amp;atext)+ (?: \. (?&amp;atext)+)*)</code>

<code>(?&lt;text&gt; [\x01-\x09\x0b\x0c\x0e-\x7f])</code>

<code>(?&lt;quoted_pair&gt; \\ (?&amp;text))</code>

<code>(?&lt;qtext&gt; (?&amp;no_ws_ctl) | [\x21\x23-\x5b\x5d-\x7e])</code>

<code>(?&lt;qcontent&gt; (?&amp;qtext) | (?&amp;quoted_pair))</code>

<code>(?&lt;quoted_string&gt; (?&amp;cfws)? (?&amp;dquote) (?:(?&amp;fws)? (?&amp;qcontent))*</code>

<code>(?&amp;fws)? (?&amp;dquote) (?&amp;cfws)?)</code>

<code>(?&lt;word&gt; (?&amp;atom) | (?&amp;quoted_string))</code>

<code>(?&lt;phrase&gt; (?&amp;word)+)</code>

<code># folding white space</code>

<code>(?&lt;fws&gt; (?: (?&amp;wsp)* (?&amp;crlf))? (?&amp;wsp)+)</code>

<code>(?&lt;ctext&gt; (?&amp;no_ws_ctl) | [\x21-\x27\x2a-\x5b\x5d-\x7e])</code>

<code>(?&lt;ccontent&gt; (?&amp;ctext) | (?&amp;quoted_pair) | (?&amp;comment))</code>

<code>(?&lt;comment&gt; \( (?: (?&amp;fws)? (?&amp;ccontent))* (?&amp;fws)? \) )</code>

<code>(?&lt;cfws&gt; (?: (?&amp;fws)? (?&amp;comment))*</code>

<code>(?: (?:(?&amp;fws)? (?&amp;comment)) | (?&amp;fws)))</code>

<code># no whitespace control</code>

<code>(?&lt;no_ws_ctl&gt; [\x01-\x08\x0b\x0c\x0e-\x1f\x7f])</code>

<code>(?&lt;alpha&gt; [a-za-z])</code>

<code>(?&lt;digit&gt; [0-9])</code>

<code>(?&lt;crlf&gt; \x0d \x0a)</code>

<code>(?&lt;dquote&gt; ")</code>

<code>(?&lt;wsp&gt; [\x20\x09])</code>

<code>)</code>

<code>(?&amp;address)/x</code>

ruby 表示,其實人家還有個簡單版本:

這樣的版本誰沒有啊——.net 說:

<code>^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$</code>

用 grep 指令在檔案中查找郵件位址,我想你不會寫個若幹行的正規表達式吧,意思一下就行了:

<code>$ grep -e -o "\b[a-za-z0-9._%+-]+@[a-za-z0-9.-]+\.[a-za-z]{2,6}\b" filename.txt</code>

在 sql server 中也是可以用正規表達式的,不過這個代碼片段應該是來自某個産品環境中的,是以,還體貼的照顧了那些把郵件位址寫錯的人:

<code>select email</code>

<code>from table_name where</code>

<code>patindex ('%[ &amp;'',":;!+=\/()&lt;&gt;]%', email) &gt; 0 -- invalid characters</code>

<code>or patindex ('[@.-_]%', email) &gt; 0 -- valid but cannot be starting character</code>

<code>or patindex ('%[@.-_]', email) &gt; 0 -- valid but cannot be ending character</code>

<code>or email not like '%@%.%' -- must contain at least one @ and one .</code>

<code>or email like '%..%' -- cannot have two periods in a row</code>

<code>or email like '%@%@%' -- cannot have two @ anywhere</code>

<code>or email like '%.@%' or email like '%@.%' -- cannot have @ and . next to each other</code>

<code>or email like '%.cm' or email like '%.co' -- camaroon or colombia? typos.</code>

<code>or email like '%.or' or email like '%.ne' -- missing last letter</code>

這個是不是有點偷懶?尤其是在那些“複雜”的正規表達式之後:

<code>from table_name</code>

<code>where regexp_like (email, '[a-z0-9._%-]+@[a-z0-9._%-]+\.[a-z]{2,4}');</code>

好吧,看來最後也一樣懶:

<code>select * from `users` where `email` not regexp '^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$';</code>

那麼,你有沒有關于驗證郵件位址的正規表達式分享給大家?

<b>原文釋出時間為:2015-08-05</b>

<b></b>

<b>本文來自雲栖社群合作夥伴“linux中國</b>