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. splunk 索引过程

    术语: Event :Events are records of activity in log files, stored in Splunk indexes. 简单说,处理的日志或话单中中一行记录 ...

  2. [转]BeginInvoke和EndInvoke方法浅析

    开发语言:C#3.0   IDE:Visual Studio 2008   一.C#线程概述   在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提 ...

  3. C#get,set

    一直对get,set的理解只在文字上:get 属性访问器用于返回属性值,而 set 访问器用于分配新值.其实这样理解是有点狭隘的,尤其是对set.set应该可以理解为为成员分配新值时的处理,比如一个类 ...

  4. call,apply,bind函数

    一.call函数 a.call(b); 简单的理解:把a对象的方法应用到b对象上(a里如果有this,会指向b) call()的用法:用在函数上面 var Dog=function(){ this.n ...

  5. 微信支付调用JSAPI缺少参数:timeStamp

    一般是安卓没问题,苹果会出现这样的问题,弹出下面这样的提示,如果你也是这样,那就恭喜你,现在,你找到解决的方法了 请看红色框框的timeStamp(图片有点小,可以鼠标右键打开图片 查看) 请注意,这 ...

  6. jquery表单提交和重置

    $('#myform').submit() 表单提交 $('#myform')[0].reset() 表单重置

  7. (DFS)hdoj1312-Red and Black

    题目链接 非常简单的DFS,初学DFS做这道题很合适.需要注意的是题目中输入的行和列顺序是颠倒的. #include<cstdio> #include<cstring> usi ...

  8. 简单探索ContentProviderOperation

    前面一片文章中用到了ContentProviderOperation,那么我们就来看看ContentProviderOperation到底是怎么工作的. 1. ContentProviderOpera ...

  9. Codeforces Round #326 (Div. 2)-Duff and Meat

    题意: Duff每天要吃ai千克肉,这天肉的价格为pi(这天可以买好多好多肉),现在给你一个数值n为Duff吃肉的天数,求出用最少的钱满足Duff的条件. 思路: 只要判断相邻两天中,今天的总花费 = ...

  10. 【转发】RedHat Enterprise Linux 6.4 使用 Centos 6 的yum源问题

    作为一名新手,学习Linux已经一个月了,其间遇到了不少问题,而今天笔者遇到的问题是 #yum install pam-devel #This system is not registered to ...