2014-03-26 91 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

你甚至可以省略'。*'(因为该模式并不固定在输入字符串的开头) –