码迷,mamicode.com
首页 > Web开发 > 详细

获取Html中所有img的src

时间:2015-11-16 18:56:57      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

/// <summary>
/// 获取所有Img中的Src
/// </summary>
/// <param name="htmlText">Html字符串文本</param>
/// <returns>以数组形式返回图片路径</returns>
public static string[] GetHtmlImageUrlList1(string htmlText)
{
Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""‘]?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""‘<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
//新建一个matches的MatchCollection对象 保存 匹配对象个数(img标签)
MatchCollection matches = regImg.Matches(htmlText);
int i = 0;
string[] sUrlList = new string[matches.Count];
//遍历所有的img标签对象
foreach (Match match in matches)
{
//获取所有Img的路径src,并保存到数组中
sUrlList[i++] = match.Groups["imgUrl"].Value;
}
return sUrlList;
}

获取Html中所有img的src

标签:

原文地址:http://www.cnblogs.com/dukechun/p/4969441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!