C#正则提取HTML中img的url值
2016-05-10 10:23
537 查看
/// <summary> /// 取得HTML中所有图片的 URL。 /// </summary> /// <param name="sHtmlText">HTML代码</param> /// <returns>图片的URL列表</returns> public static string[] GetHtmlImageUrlList(string sHtmlText) { // 定义正则表达式用来匹配 img 标签 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); // 搜索匹配的字符串 MatchCollection matches = regImg.Matches(sHtmlText); int i = 0; string[] sUrlList = new string[matches.Count]; // 取得匹配项列表 foreach (Match match in matches) sUrlList[i++] = match.Groups["imgUrl"].Value; return sUrlList; }
正则是原来开发asp.net时网上找的,现在不记得出处。谢谢原作者的分享。
相关文章推荐
- angular中的ng-bind-html指令和$sce服务
- html——网页高度
- ng-bind-html在ng-repeat中问题的解决办法
- 通过preg_replace 函数将HTML 替换成文本
- 不加<!DOCTYPE html>页面也显示,加不加的区别
- html的笔记(列表)
- HTML DOM setTimeout() 方法
- 无法关闭了HtmlUnit日志信息
- HTML基本标签
- HTML基本标签
- HTML基本标签
- [dhtmlxGant(甘特图)开发手册]第二篇——初始化、基本设置以及基本功能
- html的笔记(table表格)
- HTML基础知识三(超链接、表单)
- HTML基础知识二(页面属性、注释、表格、设置表框颜色)
- 【HTML】4.Frameset框架
- HTML布局
- 【HTML】3.Form表单
- HTML块
- 【HTML】2.head标签