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: Domain, Host, Regular Expression, URL
Posted by Xander Zelders

2 Comments:
it doesn't work
parsing "^(?(?[^:/\?#]+):)?(?//(?[^/\?#]*))?(?[^\?#]*)(?\?(?[^#]*))?(?#(?.*))?" - Unrecognized grouping construct.
Yes.. I have the same error.
Post a Comment
<< Home