Home | Index | Dotnet4all forum | Dotnet4all Snippets | Submit resources 
About | Mail us 
How to extract the host name from an URL (C#) (16 April 2007)
This snippet demonstrates how to extract the host/domain name from a valid URL using regular expressions.

  using System.Text.RegularExpressions;

...

public static string ExtractDomainFromURL(string in_URL)
{
string regexPattern = @"^(?(?[^:/\?#]+):)?(?"
+ @"//(?[^/\?#]*))?(?[^\?#]*)"
+ @"(?\?(?[^#]*))?"
+ @"(?#(?.*))?";

Regex re = new Regex(regexPattern, RegexOptions.ExplicitCapture);
Match m = re.Match(in_URL);

return m.Groups["s1"].Value + m.Groups["a1"].Value;
}

Labels: , , ,


Posted by Xander Zelders
2 Comments



 
Previous Posts
    - 9 Tips for creating indexes in SQL Server
    - Performance Tip 1: Avoid non-sargable WHERE-clause...
    - 23 Tips to improve the performance of your SQL que...
    - A factsheet for SQL Server developers
    - How to replace certain word with a hyperlink using...
    - How to Highlight a specific word in HTML content (...
    - how to extract SRC from IMG elements in HTML code
    - How to extract URL and Anchor from HTML content
    - Grab the content of a (GZIP) webpage using C#
    - How to extract the host name from an URL (C#)

Archives
    - April 2007
    - May 2007
    - April 2008


Disclaimer & Terms of Use | DotNet4All.Com concept & © 2004 - 2007 by Zelders² - Holland