public static string ReplaceOrAddImageTitle(string content, string title)
{
Regex reg = 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 mc = reg.Matches(content); string oldString = "", newString = "";
if (mc.Count > )
oldString = mc[].Value; if (oldString.IndexOf("alt=") == -)
newString = oldString.Replace("<img ", "<img alt='" + title + "' ");
content = content.Replace(oldString, newString);
return content;
}
 public static string ReplaceOrAddImageTitle1(string content, string title)
{
int startIndex = content.IndexOf("<img ");
int endIndex = content.IndexOf(">", startIndex);
string oldString = content.Substring(startIndex, endIndex - startIndex + );
string newString = ""; if (oldString.IndexOf("alt=") == -)
newString = oldString.Replace("<img ", "<img alt='" + title + "' ");
else
{
startIndex = oldString.IndexOf("alt");
int index1 = oldString.IndexOf("'");
int index2 = oldString.IndexOf("\"");
if (index1 < index2)
endIndex = oldString.IndexOf("'", index1 + );
else
endIndex = oldString.IndexOf("\"", index2 + ); string altStr = oldString.Substring(startIndex, endIndex - startIndex + );
newString = oldString.Replace(altStr, " ").Replace("<img ", "<img alt='" + title + "' ");
}
content = content.Replace(oldString, newString);
return content;
}


用正则表达式获取所有img标签的更多相关文章

  1. [转载]C#用正则表达式 获取网页源代码标签的属性或值

    最近调试程序需要用到获取网页指定标签的属性和值,找到了一个比较好的正则匹配方法,特此备份. [原]C#用正则表达式 获取网页源代码标签的属性或值 整理两个 在C#中,用正则表达式 获取网页源代码标签的 ...

  2. java正则表达式获取指定HTML标签的指定属性值

    package com.mmq.regex; import java.util.ArrayList; import java.util.List; import java.util.regex.Mat ...

  3. C#用正则表达式 获取网页源代码标签的属性或值

    1.有url获取到网页源代码: using System.Web; using System.IO; using System.Net; private void GetHtmlinfo(string ...

  4. C#用正则表达式 获取标签的属性或值

    整理两个 在C#中,用正则表达式 获取网页源代码标签的属性或值的方法 : 1.获取标签中的值: string str="<a href=\"www.csdn.net\&quo ...

  5. 随手记一次用C#正则表达式获取下拉菜单html标签<select>以及相关属性值

    随手记一次用C#正则表达式获取下拉菜单html标签<select>以及相关属性值 1:有如下html: .................. <select id="aaa ...

  6. C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值

    //C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值,表达式如下: Regex regImg = new Regex(@"(?is)<a[^>]*?href ...

  7. 使用C#正则表达式获取必应每日图片地址

    微软的Bing搜索引擎首页每天都会提供了一些有趣的图片,下面使用正则表达式获取图片的地址,不管是在手机app还是在网站上都是很好的图片素材,而且每天更新,非常不错. 首先访问微软的API,该地址返回的 ...

  8. vim 正则表达式获取双引号中的字符

    vim 正则表达式获取双引号中的字符   1.获取双引号中的字符 :%s/.*\".∗\".*/\1/ 2.用字符串建立标签 如 hello  <hello></ ...

  9. php正则获取html图片标签信息(采集图片)

    php获取html图片标签信息(采集图片),实现图片采集及其他功能,带代码如下: <?php $str="<img src='./a.jpg'/>111111<img ...

随机推荐

  1. 数据库索引<二> 补充前篇 (上一篇抽风了,这个补上)

    在前一个创建索引中已经大概说了三部分的影响,基本应该注意哪一些.写完上一篇后我感觉有很多地方没有写清楚,所以这篇就是更深入一些的理解索引到底是怎么和数据表关联,怎么快速查询的. 先看一下下面的图,图是 ...

  2. 经验分享:使用 Restyle.js 简化 CSS 预处理

    Andrea Giammarchi的restyle.js是一个新的,基于JavaScript的CSS预处理器,能够运行在服务端(通过Node.js)或者浏览器中.它宣称自己是“一种简化的CSS方法”, ...

  3. AjaxUpload.3.5.js之ASP.NET 文件上传

    一.引入js文件 <script type="text/javascript" src="/Scripts/JQuery.min.js"></ ...

  4. 下拉刷新列表添加SwipeDismissListViewTouchListener实现滑动删除某一列。

    <Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github上的 ...

  5. hover用法

    $('.F_box_2').hover(            function(){ $(this).find(".make_reply").css({"color&q ...

  6. Js笔试题之parseInt()和.map()

    parseInt()的几个例子 var b = parseInt("01"); alert("b="+b); var c = parseInt("09 ...

  7. javascrip自定义对象的方式

    对象初始化方式(也叫json对象创建方式) <script type="text/javascript"> var User = { name:"paul&q ...

  8. android程序打包成APK

    1.下载ant(从官网上下载没有bin目录,可以直接在百度上搜APACHE-ANT-1.9.4-BIN.ZIP) 2.解压到C盘根目录(也可以是其他盘) 3.修改环境变量 ANT_HOME    C: ...

  9. php base64_decode 解码方法

    <?php header('Content-Type:text/html;charset=utf-8'); function encode_file_contents($filename) { ...

  10. IDEA调试javaScript

            谈起JavaScript调试,大家可能想到的就是FireFox下的FireBug,这毫无疑问,FireBug基本已经成为JavaScript开发人员的必备工具.在本文中,将向大家介绍如 ...