目前我在 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/&sa=U&ved=0CBUQFjAAahUKEwi_koaWptjHAhXG8HIKHYcQCl0&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.\\-?=/:]*)";
我不确定正则表达式是否返回您想要的内容,因为我无法从您的描述中理解您要匹配的内容。但可以肯定的是,未泄露的 "?" 是一个问题。
