用正则表达式获取所有img标签
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标签的更多相关文章
- [转载]C#用正则表达式 获取网页源代码标签的属性或值
最近调试程序需要用到获取网页指定标签的属性和值,找到了一个比较好的正则匹配方法,特此备份. [原]C#用正则表达式 获取网页源代码标签的属性或值 整理两个 在C#中,用正则表达式 获取网页源代码标签的 ...
- java正则表达式获取指定HTML标签的指定属性值
package com.mmq.regex; import java.util.ArrayList; import java.util.List; import java.util.regex.Mat ...
- C#用正则表达式 获取网页源代码标签的属性或值
1.有url获取到网页源代码: using System.Web; using System.IO; using System.Net; private void GetHtmlinfo(string ...
- C#用正则表达式 获取标签的属性或值
整理两个 在C#中,用正则表达式 获取网页源代码标签的属性或值的方法 : 1.获取标签中的值: string str="<a href=\"www.csdn.net\&quo ...
- 随手记一次用C#正则表达式获取下拉菜单html标签<select>以及相关属性值
随手记一次用C#正则表达式获取下拉菜单html标签<select>以及相关属性值 1:有如下html: .................. <select id="aaa ...
- C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值
//C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值,表达式如下: Regex regImg = new Regex(@"(?is)<a[^>]*?href ...
- 使用C#正则表达式获取必应每日图片地址
微软的Bing搜索引擎首页每天都会提供了一些有趣的图片,下面使用正则表达式获取图片的地址,不管是在手机app还是在网站上都是很好的图片素材,而且每天更新,非常不错. 首先访问微软的API,该地址返回的 ...
- vim 正则表达式获取双引号中的字符
vim 正则表达式获取双引号中的字符 1.获取双引号中的字符 :%s/.*\".∗\".*/\1/ 2.用字符串建立标签 如 hello <hello></ ...
- php正则获取html图片标签信息(采集图片)
php获取html图片标签信息(采集图片),实现图片采集及其他功能,带代码如下: <?php $str="<img src='./a.jpg'/>111111<img ...
随机推荐
- ios基础篇(九)——自定义UITabBar
上一篇讲到了UITabBarViewController,接着说说UITabBarViewController中怎么自定义TabBar. 今天仿写了微博,发现底部tabbar中间的button和其他有 ...
- LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- 重点关注之OData with List
OData是什么 官方解释:The Open Data Protocol (OData) is a data access protocol for the web. OData provides a ...
- mysql有回滚,php没有回滚的说法
mysql 事务表是有回滚的说法.当发生mysql层面的错误才会执行回滚
- NSString length的坑。
说坑,可能过头了,是我理所当然的把OC看作C了, char* cstr = "zh中文12"; NSString* s = [NSString stringWithUTF8Stri ...
- 找出html中的图片、包括css中的图片,读出图片数据转换为base64数据
<?php echo ">> 图片的地址,css里面的要打单引号\r\n"; echo ">> 相同的图片,使用css实现图片地址只出现一次 ...
- 常州培训 day5 解题报告
第一题:(贪心) 题目大意:给出N*M的矩形,要用正方形将它铺满(正方形之间不能重叠),相邻的正方形颜色不能相同,颜色用ABCD表示.要求从上到下从左到右字典序最小. N,M<=100 解题过程 ...
- 使用ASP.Net WebAPI构建REST服务(三)——返回值
Asp.Net WebAPI服务函数的返回值主要可以分为void.普通对象.HttpResponseMessag.IHttpActionResult e四种,本文这里简单的介绍一下它们的区别. 一.返 ...
- 创建一个ROS msg
1. msg •msg:msg文件是简单的文本文件,用于描述ROS中消息(消息的各个参数项).用于为不同的编程语言生成有关消息的源代码. •srv:描述服务的文件,由两部分组成:请求和反馈: msg文 ...
- Linux-Unix版本介绍
转自: http://blog.163.com/li_zhuangs/blog/static/195698098201182411360635/ < DOCTYPE html PUBLIC -W ...