取页面中的一段字符串 正策筛选

小城山 2013-05-13 09:52:37
MatchCollection mc = Regex.Matches(Search_Html_DanPin, @"(?is)(?<=class=""Pic""\s\W*\shref="").*?(?=product_id=)");
//Search_Html_DanPin 是获取的一个网页内容
// @"(?is)(?<=class=""Pic""\s\W*\shref="").*?(?=product_id=)") 是筛选的正侧表达式

// 比如 页面中的一段:class="pic" href="http://product.dangdang.com/product.aspx //product_id=22832334#ddclick //act=click&pos=22832334_16_1_q&cat=&key=9787538460131&qinfo=21_1_48&pinfo=&minfo=&ninfo=&custid//=&permid=20130510161738906174469583512667327&ref=&rcount=&type=&t=1368342485000" //target="_blank"

//要取product_id的值 下面的代码只取到了符合规则的数量 但是没取到值 请教如何写(正策我刚接触)

string[] res = new string[mc.Count];
for (int i = 0; i < res.Length; i++)
{
res[i] = "";
}
for (int i = 0; i < mc.Count; i++)
{
res[i] = mc[i].Value.ToString();
}
return res;
...全文
245 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
EnForGrass 2013-05-13
  • 打赏
  • 举报
回复
(?is)(?<=class="pic"\s\W*\shref=").*?product_id=(\d+)(?=#)

   string[] res = new string[mc.Count];
            for (int i = 0; i < res.Length; i++)
            {
                res[i] = "";
            }
            for (int i = 0; i < mc.Count; i++)
            {
                res[i] = mc[i].Groups[1].Value.ToString();
            }
            return res;
小城山 2013-05-13
  • 打赏
  • 举报
回复
res[i] = mc[i].Value.ToString(); mc[i].Value都是 http://product.dangdang.com/product.aspx? 取错了值
小城山 2013-05-13
  • 打赏
  • 举报
回复
只要也至少要匹配class="pic" href="http://product.dangdang.com/product.aspx //product_id=
小城山 2013-05-13
  • 打赏
  • 举报
回复
必须要筛选 class="pic" 因为 页面中有很多这种结构的字符串 可能会取到不需要的字段
bdmh 2013-05-13
  • 打赏
  • 举报
回复
@"(?is)(?<=product_id=)[\d]+(?=#)";

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧