2010-02-17 70 views
3

我正在使用電子郵件格式的正則表達式,但我認爲這是正常的,但客戶抱怨表達式太嚴格。所以他們回來了以下要求:一些電子郵件規則的正則表達式

電子郵件必須包含一個「@」符號,以.xx或.xxx即。(.nl或.com)結尾。他們很高興通過驗證。我已經開始表達,看是否字符串包含「@」符號,如下

^(?=。* [@])

這似乎工作,但我怎麼添加最後要求(必須以.xx或.xxx結尾)?

+0

取決於使用的語言,請嘗試:\ \ W {2,3} $ 但也有一些domaind結局有3點以上的字符: * .info,... – tur1ng

+1

@ tur1ng:而且,確實是.museum。 –

+0

+1:從來沒有聽說過那個;-) – tur1ng

回答

2

一個正則表達式只是執行你的兩個要求是:

^[email protected]+\.[a-zA-Z]{2,3}$ 

但是,大多數語言都有電子郵件驗證庫,通常比正則表達式工作得更好。

1

試試這個:

([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})\be(\w*)s\b 

一個很好的工具來測試我們的正則表達式: http://gskinner.com/RegExr/

+0

好的答案,主要是,gskinner的RegExp測試人員也是我使用的。我很好奇爲什麼你在最後包含了字面「e」+任何空格+「s」。例如,您的表情將在[email protected]上失敗。簡單地使用([\ w - \。] +)@((?:[\ w] + \。)+)([a-zA-Z] {2,4})\ b將會成功。 – Robusto

0

你可以使用

[@].+\.[a-z0-9]{2,3}$ 
+0

頂級域名允許使用數字嗎? – tur1ng

+0

IP在技術上被允許在電子郵件中...這就是爲什麼我添加了0-9。 – Dominik

2

我一直用這個電子郵件

  ^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + 
      @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + 
      @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$ 

嘗試http://www.ultrapico.com/Expresso.htm以及!

2

這是不可能驗證每個電子郵件地址正則表達式,但爲您的要求這個簡單的正則表達式工作。這既不是完整的也不是在錯誤的任何檢查,但它恰恰滿足規格:

[^@][email protected]+\.\w{2,3}$ 

說明:

  • [^ @] +:匹配一個或多個字符不屬於@
  • @:匹配@
  • +:匹配一個或多個任意字符
  • \的:匹配一個。
  • \ W {2,3}:匹配2或3字字符(A-ZA-Z)
  • $:字符串
0

結束這應該工作:

^[^@\r\n\s]+[^[email protected]]@[^[email protected]][^@\r\n\s]+\.(\w){2,}$ 

我測試了針對這些無效的電子郵件:

@[email protected] 
[email protected] 
exampledomain.com 
[email protected] 
[email protected] 
[email protected]@com 

[email protected] 

[email protected] 
[email protected]@il.company.co 
[email protected]@internal-email.company.co 

@test.com 
[email protected] 
[email protected]  
[email protected] 

而且這些有效的電子郵件:

[email protected] 
[email protected] 
[email protected] 

編輯

這一個似乎證實了所有從維基百科頁面的地址,但它可能使一些無效的電子郵件爲好。括號將它分割成的一切前,@後面:

^([^\r\n]+)@([^\r\n]+\.?\w{2,})$ 

[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected][IPv6:2001:db8:1ff::a0b:dbd0] 
"much.more unusual"@example.com 
"[email protected]"@example.com 
"very.(),:;<>[]\".VERY.\"[email protected]\\ \"very\".unusual"@strange.example.com 
[email protected] 
[email protected] 
!#$%&'*+-/=?^_`{}|[email protected] 
"()<>[]:,;@\\\"!#$%&'*+-/=?^_`{}| ~.a"@example.org 
" "@example.org 
üñîçøðé@example.com 
üñîçøðé@üñîçøðé.com 
+2

你應該看看:http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses和http://data.iana.org/TLD/tlds-alpha-by-domain.txt – Toto

+0

哇謝謝你。這正是我所期待的。在我開始製作正則表達式之前,可能應該使用google搜索。 – Brett

相關問題