请教各位大侠,小弟现在写了个小程序,想用正则表达式抓取网页上的内容,比如csdn首页的热点资讯排行那一小块的内容,如果匹配的是<table[\s\S]*?/table>,匹配的结果需要自己手动验证结果里面含不含关键字"热点咨询",如何在正则里面直接实现?直接抓取含有关键字"热点咨询"table?
注:首页里用的不是table标签,举例说明而已
希望大家帮帮俺.谢谢各位了.
注:首页里用的不是table标签,举例说明而已
希望大家帮帮俺.谢谢各位了.
string pattern = "<table[^>]*?>(?<keyword>.*?)</table>";
Regex regex = new Regex(pattern);
MatchCollection mc = regex.Matches(str);
foreach (Match m in mc)
Console.WriteLine(m.Groups["keyword"].Value);
Console.ReadKey();
认真看看,正则是经常要用的,学会如何用,而不是向别人求一段现成的表达式,程序员,不该这样
Regex reg = new Regex("<a>(?:(?!</?a).)*2(?:(?!</?a).)*</a>");
Console.WriteLine(reg.Match(str).Value);
string keyword = "2";
string pattern = @"<a[^>]*>[^<]+"+keyword+"[^<]*</a>";
foreach (Match m in Regex.Matches(tempStr,pattern))
{
//循环输出
string value = m.Value;
//<a>123</a>
}
MatchCollection matchs = Regex.Matches(text, @"<a>(?<data>\d*[2]+\d*[^</a>]*)</a>");
foreach (Match mh in matchs)
Console.WriteLine(string.Concat("regex:", mh.Groups[0]," Value:", mh.Groups["data"]));
foreach (Match m in reg.Matches(str))
{
Response.Write(m.Value + "<br/>");
}这里,你只需要把那个a 换成你要匹配的标签就可以了。