如何从字符串中获取域名

本文关键字:获取 域名 字符串 | 更新日期: 2023-09-27 18:26:57

我需要你的帮助。我想在我的C#应用程序中从字符串中分离域名。对此有任何想法。

例如:string strURL="http://stackoverflow.com/questions";

我需要像这样的输出域名:stackoverflow.com

如何从字符串中获取域名

这应该可以工作。

新Uri("http://stackoverflow.com/questions").DnsSafeHost

您可以使用Regex。。。。

            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提供域名。。。。。