Monday 20 July 2009

Extracting the Extension of a Url Host

var host = this.Request.Url.Host;
private string ExtractExtension(string host)
{
string result = null;
var pattern = @"[\w-]+\.[\w-]+(?<extension>(\.[\w-]+)+)";
var match = Regex.Match(host , pattern);
if (match.Groups["extension"].Success)
{
result = match.Groups["extension"].Value;
}
return result;
}
Note: this method is easily testable.

No comments: