简体   繁体   中英

Regex Match Domain Extension

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

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

Actually, the given answer misses the url such as something.org/default.html . Therefore, to deal with that I propose the following regex pattern instead:

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

You can test that here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM