2014-03-26 89 views
0

我需要確認域擴展名是否存在。正則表達式匹配域擴展

到目前爲止,我還沒有能夠得到一個匹配域擴展

凡域名可以有通配符:gmail.com,msn.com,mac.com,comcast.net

DomainPartOfEmail = Right(temp, (Len(temp) - temp.LastIndexOf("@") - 1)) 
    If Regex.IsMatch(DomainPartOfEmail, "*.edu? | *.com? | *.net? | *.org?", RegexOptions.IgnoreCase) Then 
     ValidDomain = True 
    End If 

回答

1

如果域只能從這些(EDU,COM,淨,組織),然後使用這一個:

".*\.(edu|com|net|org)$" 

正則表達式的說明:

NODE      EXPLANATION 
-------------------------------------------------------------------------------- 
    .*      any character except \n (0 or more times 
          (matching the most amount possible)) 
-------------------------------------------------------------------------------- 
    \.      '.' 
-------------------------------------------------------------------------------- 
    (      group and capture to \1: 
-------------------------------------------------------------------------------- 
    edu      'edu' 
-------------------------------------------------------------------------------- 
    |      OR 
-------------------------------------------------------------------------------- 
    com      'com' 
-------------------------------------------------------------------------------- 
    |      OR 
-------------------------------------------------------------------------------- 
    net      'net' 
-------------------------------------------------------------------------------- 
    |      OR 
-------------------------------------------------------------------------------- 
    org      'org' 
-------------------------------------------------------------------------------- 
)      end of \1 
-------------------------------------------------------------------------------- 
    $      before an optional \n, and the end of the 
          string 
+2

你甚至可以省略'。*'(因爲該模式並不固定在輸入字符串的開頭) –