Regex Match Domain Extension

爷,独闯天下 提交于 2021-01-29 06:51:13

问题


I need to confirm that the domain extension is present.

So far I have not been able to get a match for the domain extension

Where the domain name can have wild cards: 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:


If the domains are only from these(edu, com, net, org) then use this one:

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

Explanation of the regex:

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


来源:https://stackoverflow.com/questions/22671419/regex-match-domain-extension

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!