C#正则表达式获取网址的域名(IP)

代码如下:

string p = @"(http|https)://(?<domain>[^(:|/]*)";
Regex reg = new Regex(p, RegexOptions.IgnoreCase);
Match m = reg.Match(HostUrl);
Result=m.Groups["domain"].Value;

如http://localhost:8733/HVMsg/HVWcfService/获取的是:localhost

如http://guonei.news.baidu.com/n?cmd=1&class=shizheng获取的是:guonei.news.baidu.com

 

posted @ 2015-04-21 09:16  rabtor  阅读(4701)  评论(0编辑  收藏  举报