Regex to extract a second level domain from a fully qualified domain name?

I can’t figure it out. I need to extract a second level domain from a fully qualified domain name. For example, all of them should return "example.com":

  • example.com
  • foo.example.com
  • bar.foo.example.com
  • example.com:8080
  • foo.example.com:8080
  • bar.foo.example.com:8080

Here is what I still have:

    Dim host = Request.Headers("Host")
    Dim pattern As String = "(?<hostname>(\w+)).(?<domainname>(\w+.\w+))"
    Dim theMatch = Regex.Match(host, pattern)
    ViewData("Message") = "Domain is: " + theMatch.Groups("domainname").ToString

It does not work for example.com:8080and bar.foo.example.com:8080. Any ideas?

+3
source share
3 answers

I have successfully used this Regex to match "example.com" from your list of test cases.

"(?<hostname>(\w+\.)*)(?<domainname>(\w+\.\w+))"

The dot character (".") Must be escaped as "\.". "." a character in a regular expression pattern matches any character.

1 , ( "(? (\ w +)).". , ). "example.com", .

, "1 , ". "foo" "foo.example.com" "foo.bar" "foo.bar.example.com".

+4

, fqdn (: , , - ), .

'(?:(?<hostname>.+)\.)?(?<domainname>[^.]+\.[^.]+?)(?:\:(?<port>[^:]+))?$'

, ( ):

bar.foo.example.com:8000 :

  • : bar.foo()
  • : example.com
  • : 8000 ()
+2

VB.NET ASP, ...

  • , ^ $.
  • \w , .., . , \w , .
  • , , .

I am sure there is more RFC-exact expression there, but here begins something that should work for you.

^([a-z0-9\-]+\.)*([a-z0-9\-]+\.[a-z0-9\-]+)(:[0-9]+)?$

Broken:

([a-z0-9\-]+\.)*: Start from zero or more host names ...
([a-z0-9\-]+\.[a-z0-9\-]+): it is followed by two host names ...
(:[0-9]+)?: it is followed by an optional port declaration.

Please note that if you are dealing with a type domain example.ne.jp, you will only get .ne.jp. Also, note that the above expression must be case insensitive.

+1
source

All Articles