更新时间:2023-02-23 18:01:50
与其依赖不可靠的正则表达式,不如使用 System.Uri
为您进行解析.使用这样的代码:
Rather than relying on unreliable regex use System.Uri
to do the parsing for you. Use a code like this:
string uriStr = "www.foo.com";
if (!uriStr.Contains(Uri.SchemeDelimiter)) {
uriStr = string.Concat(Uri.UriSchemeHttp, Uri.SchemeDelimiter, uriStr);
}
Uri uri = new Uri(uriStr);
string domain = uri.Host; // will return www.foo.com
现在获取您可以使用的***域:
string tld = uri.GetLeftPart( UriPartial.Authority ); // will return foo.com