gpt4 book ai didi

c# - Google 搜索引擎排名正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:44 24 4
gpt4 key购买 nike

目前我在 C# 中的正则表达式是

string lookup = "(<h3 class=\"r\"><a href=\"/url?q=)(\\w+[a-zA-Z0-9.\\-?=/:]*)";

我正在尝试匹配如下所示的谷歌搜索引擎排名结果

<h3 class="r"><a href="/url?q=https://uk.yahoo.com/&amp;sa=U&amp;ved=0CBUQFjAAahUKEwi_koaWptjHAhXG8HIKHYcQCl0&amp;usg=AFQjCNHkhixstCvIO45GIKI44_kp6ul36w">

这段代码功能更详细

string lookup = "(<h3 class=\"r\"><a href=\"/url?q=)(\\w+[a-zA-Z0-9.\\-?=/:]*)";
MatchCollection matches = Regex.Matches(html, lookup);

for (int i = 0; i < matches.Count; i++)
{
string match = matches[i].Groups[2].Value;
if (match.Contains(url.Host))
return i + 1;
}

return 0;

我遇到的错误是我总是得到“0”

最佳答案

上面正则表达式的问题是你忘记转义 ?

string lookup = "(<h3 class=\"r\"><a href=\"/url\\?q=)(\\w+[a-zA-Z0-9.\\-?=/:]*)";

我不确定正则表达式是否返回您想要的内容,因为我无法从您的描述中理解您要匹配的内容。但可以肯定的是,未泄露的 "?" 是一个问题。

关于c# - Google 搜索引擎排名正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32352949/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com