首页 > Web开发 > 详细

提取html中的src 路径

时间:2015-03-20 17:52:03      阅读:324      评论:0      收藏:0      [点我收藏+]
 1  /// <summary>
 2         /// 替换body中的img src属性 附加上域名
 3         /// </summary>
 4         /// <param name="str"></param>
 5         /// <returns></returns>
 6         protected virtual string ReplaceImg(string str)
 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(str);
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;
20             foreach (var item in sUrlList)
21             {
22                 string newStr = _storeContext.CurrentStore.Url + item.Substring(item.IndexOf(/)+1);
23                 str = str.Replace(item, newStr);
24             }
25             return str;
26         }

 

提取html中的src 路径

原文:http://www.cnblogs.com/Jnw-qianxi/p/4353841.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!