Regex for extracting second level domain from FQDN?
Posted
by Bob
on Stack Overflow
See other posts from Stack Overflow
or by Bob
Published on 2009-11-21T04:45:06Z
Indexed on
2010/06/10
1:12 UTC
Read the original article
Hit count: 356
asp.net-mvc
|vb.net
I can't figure this out. I need to extract the second level domain from a FQDN. For example, all of these need to 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's what I have so far:
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 fails for example.com:8080
and bar.foo.example.com:8080
. Any ideas?
© Stack Overflow or respective owner