简体   繁体   English

如何从字符串中获取域名

[英]How to get domain name from a String

I need a help of you.我需要你的帮助。 I want to split domain name from a string in my C# application.我想从我的 C# 应用程序中的字符串中拆分域名。 Any idea about that.任何想法。

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

and I need output like DomainName : stackoverflow.com我需要像 DomainName 这样的输出:stackoverflow.com

This should work.这应该有效。

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

You can do it using 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 gives the domain name..... domainName 给出域名......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM