我想提取顶级域名和国家顶级域名从字符串与正则表达式。我测试了许多如下代码的正则表达式:
var linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Match m = linkParser.Match(Url);
Console.WriteLine(m.Value);但是这些代码都不能正确地做到这一点。用户输入的文本字符串可以是以下语句:
jonasjohn.com
http://www.jonasjohn.de/snippets/csharp/
jonasjohn.de
www.jonasjohn.de/snippets/csharp/
http://www.answers.com/article/1194427/8-habits-of-extraordinarily-likeable-people
http://www.apple.com
https://www.cnn.com.au
http://www.downloads.news.com.au
https://ftp.android.co.nz
http://global.news.ca
https://www.apple.com/
https://ftp.android.co.nz/
http://global.news.ca/
https://www.apple.com/
https://johnsmith.eu
ftp://johnsmith.eu
johnsmith.gov.ae
johnsmith.eu
www.jonasjohn.de
www.jonasjohn.ac.ir/snippets/csharp
http://www.jonasjohn.de/
ftp://www.jonasjohn.de/
https://subdomain.abc.def.jonasjohn.de/test.htm我测试的正则表达式:
^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)"
\b(?:https?://|www\.)\S+\b
://(?<host>([a-z\\d][-a-z\\d]*[a-z\\d]\\.)*[a-z][-a-z\\d]+[a-z])还有太多,我只需要域名,我不需要一个协议或子域。如: Domainname.gTLD、DomainName.ccTLD或DomainName.xyz.ccTLD
我从PUBLIC SUFFIX上拿到了他们的名单
当然,我在stackoverflow.com上看到了很多帖子,但都没有回应我。
发布于 2021-07-05 21:49:16
根据Lidqy answer的说法,我编写了这个函数,我认为它支持大多数可能的情况,如果输入值不在这个范围内,您可以将其作为一个例外。
public static string ExtractDomainName(string Url)
{
var regex = new Regex(@"^((https?|ftp)://)?(www\.)?(?<domain>[^/]+)(/|$)");
Match match = regex.Match(Url);
if (match.Success)
{
string domain = match.Groups["domain"].Value;
int freq = domain.Where(x => (x == '.')).Count();
while (freq > 2)
{
if (freq > 2)
{
var domainSplited = domain.Split('.', 2);
domain = domainSplited[1];
freq = domain.Where(x => (x == '.')).Count();
}
}
return domain;
}
else
{
return String.Empty;
}
}发布于 2021-07-05 20:13:44
您不需要使用正则表达式来解析URL。如果您有一个有效的URL,您可以使用Uri构造函数或Uri.TryCreate来解析它:
if(Uri.TryCreate("http://google.com/asdfs",UriKind.RelativeOrAbsolute,out var uri))
{
Console.WriteLine(uri.Host);
}不过,www.jonasjohn.de/snippets/csharp/和jonasjohn.de/snippets/csharp/不是有效的URL。TryCreate仍然可以将它们解析为相对URL,但是读取Host会抛出System.InvalidOperationException: This operation is not supported for a relative URI.
在这种情况下,您可以使用UriBuilder类来解析和修改URL,例如:
var bld=new UriBuilder("jonasjohn.com");
Console.WriteLine(bld.Host);这是打印
jonasjohn.com设置Scheme属性会生成一个有效的、完整的URL:
bld.Scheme="https";
Console.WriteLine(bld.Uri);这会产生以下结果:
https://jonasjohn.com:80/发布于 2021-07-05 20:09:20
var rx = new Regex(@"^((https?|ftp)://)?(www\.)?(?<domain>[^/]+)(/|$)");
var data = new[] { "jonasjohn.com",
"http://www.jonasjohn.de/snippets/csharp/",
"jonasjohn.de",
"www.jonasjohn.de/snippets/csharp/",
"http://www.answers.com/article/1194427/8-habits-of-extraordinarily-likeable-people",
"http://www.apple.com",
"https://www.cnn.com.au",
"http://www.downloads.news.com.au",
"https://ftp.android.co.nz",
"http://global.news.ca",
"https://www.apple.com/",
"https://ftp.android.co.nz/",
"http://global.news.ca/",
"https://www.apple.com/",
"https://johnsmith.eu",
"ftp://johnsmith.eu",
"johnsmith.gov.ae",
"johnsmith.eu",
"www.jonasjohn.de",
"www.jonasjohn.ac.ir/snippets/csharp",
"http://www.jonasjohn.de/",
"ftp://www.jonasjohn.de/",
"https://subdomain.abc.def.jonasjohn.de/test.htm"
};
foreach (var dat in data) {
var match = rx.Match(dat);
if (match.Success)
Console.WriteLine("{0} => {1}", dat, match.Groups["domain"].Value);
else {
Console.WriteLine("{0} => NO MATCH", dat);
}
}https://stackoverflow.com/questions/68255680
复制相似问题