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:

Luca said...

it doesn't work

parsing "^(?(?[^:/\?#]+):)?(?//(?[^/\?#]*))?(?[^\?#]*)(?\?(?[^#]*))?(?#(?.*))?" - Unrecognized grouping construct.

May 7, 2007 5:15 PM  
Bibin said...

Yes.. I have the same error.

July 6, 2007 6:44 PM  

Post a Comment

<< Home

 
Previous Posts
    - How to Send an email using SMTP (C#)
    - How to remove HTML-tags from web content (C#)
    - How to convert DateTime to SQL valid string



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