7

Out customers can enter websites from domain names. They also can enter mailadresses from their contacts.

Know we need to find customers which websited whoose domain can be associated to the domains of the mailadresses.

So my idea is to extract the host from the webadress and from the url and compare them

So what's the most reliable algorithm to get the hostname from a url?

for example a host can be:

foo.com
www.foo.com
http://foo.com
https://foo.com
https://www.foo.com

The result should always be foo.com

2
  • point of clarification, since you deleted the example with the .vu TLD are you saying you only care about .com TLDs or is this an oversimplification? Commented May 24, 2012 at 10:36
  • it's an oversimplification. it could be any kind of TLD, .de .eu .biz..... the important requirement is to find possible candidates matching mailadresses by looking at website urls Commented May 24, 2012 at 10:39

4 Answers 4

16

Rather than relying on unreliable regex use System.Uri to do the parsing for you. Use a code like this:

string uriStr = "www.foo.com";
if (!uriStr.Contains(Uri.SchemeDelimiter)) {
    uriStr = string.Concat(Uri.UriSchemeHttp, Uri.SchemeDelimiter, uriStr);
}
Uri uri = new Uri(uriStr);
string domain = uri.Host; // will return www.foo.com

Now to get just the top-level domain you can use:

string tld = uri.GetLeftPart( UriPartial.Authority ); // will return foo.com
Sign up to request clarification or add additional context in comments.

3 Comments

shouldn't tld result in just "com" ?
@anubhava: uri.GetLeftPart(UriPartial.Authority) does not return the root domain name. Instead it returns the entire left part of the URL, starting from the scheme and ending with the port (if specified). AFAIK, the only way to ignore the sub-domain portion of the host is to explicitly truncate it using a 2-pass call to string.LastIndexOf().
I can confirm (certainly in aspnetcore 3.1) string tld = uri.GetLeftPart( UriPartial.Authority ); // will NOT return foo.com here it will return www.foo.com (same for non-www subdomains)
1

Here's a regular expression that will match the url's you have provided. Basically http and https etc are optional, as is the www Everything is then matched up to a possible path;

var expression = /(https?:\/\/)?(www\.)?([^\/]*)(\/.*)?$/;

This would mean that;

var result = 'https://www.foo.com.vu/blah'.replace(expression, '$3')

Would evaluate to

result === 'foo.com.vu'

3 Comments

the question is what about subdomains. i think they should not be included in the result. so product.mycompany.com should end up in mycompany.com
That could be quite difficult as you couldn't count the dots to amuse a sub-domain (I guess what I'm trying to say is things like .co.uk would mess things up). You'd probably have to do two checks, one with the expression above and one that strips of the char's before the first dot
This answer fails if you evaluated a DNS name with invalid characters (such as a!notit.com), or one with too many characters (over 63)
1

There is already a url parser in c# for extracting this information

Here are some examples http://www.stev.org/post/2011/06/27/C-HowTo-Parse-a-URL.aspx

Comments

0

See this url. The Host property, unlike the Authority will not include the port number.

http://msdn.microsoft.com/en-us/library/system.uri.host(v=vs.110).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.