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

取得HTML中所有图片的 URL 正则表达式

时间:2014-09-23 21:17:15      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   sp   art   cti   on   c   代码   

  1. /// <summary>   
  2. /// 取得HTML中所有图片的 URL。   
  3. /// </summary>   
  4. /// <param name="sHtmlText">HTML代码</param>   
  5. /// <returns>图片的URL列表</returns>   
  6. public static string[] GetHtmlImageUrlList(string sHtmlText)   
  7. {   
  8.   // 定义正则表达式用来匹配 img 标签   
  9.   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);   
  10.     
  11.   // 搜索匹配的字符串   
  12.   MatchCollection matches = regImg.Matches(sHtmlText);   
  13.   int i = 0;   
  14.   string[] sUrlList = new string[matches.Count];   
  15.     
  16.   // 取得匹配项列表   
  17.   foreach (Match match in matches)   
  18.   sUrlList[i++] = match.Groups["imgUrl"].Value;   
  19.   return sUrlList;   

取得HTML中所有图片的 URL 正则表达式

标签:io   ar   for   sp   art   cti   on   c   代码   

原文地址:http://www.cnblogs.com/wahaccp/p/3989063.html

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