[英]How to get domain name from a String
我需要你的帮助。 我想从我的 C# 应用程序中的字符串中拆分域名。 任何想法。
例如: string strURL="http://stackoverflow.com/questions";
我需要像 DomainName 这样的输出:stackoverflow.com
这应该有效。
新的 Uri("http://stackoverflow.com/questions").DnsSafeHost
您可以使用正则表达式来做到这一点......
string domainName = string.Empty;
string strURL="http://stackoverflow.com/questions";
Regex rg = new Regex("://(?<host>([a-z\\d][-a-z\\d]*[a-z\\d]\\.)*[a-z][-a-z\\d]+[a-z])");
if (rg.IsMatch(strURL))
{
domainName = rg.Match(strURL).Result("${host}");
}
domainName 给出域名......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.