如何用正则批量替换一篇文章中的图片链接地址并保存所有图片链接地址到ArrayList

cisky 2008-04-20 07:32:27
如题。为了说明这个问题,我从NEWS.QQ上面复制了一段HTML代码,如下:
<label id="ArticleCnt" runat="server"><P align=center><FONT size=2><IMG alt=这是标题 src="../pics/9943/9943444.jpg" border=0 name=MM></FONT></P>
<DIV style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体" align=center>
<P style="TEXT-INDENT: 2em">这是图片标题。 </P></DIV>
<P align=center><FONT size=2><IMG src="../pics/9943/9943448.jpg" border=0></FONT></P>
<DIV style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体" align=center>图片标题</DIV>
<P align=center><FONT size=2><IMG src="../pics/9943/9943455.jpg" border=0></FONT></P>
</label>

现在我想从这段代码中查找所有的图片链接,将../pics/替换成../images/并同时将所有图片的绝对地址存入一个数组,如下:
//ArrayList _arrayList = new ArrayList();
http://img1.qq.com/news/pics/9943/9943444.jpg
http://img1.qq.com/news/pics/9943/9943448.jpg
http://img1.qq.com/news/pics/9943/9943455.jpg
我想将这些地址存入一个数组中供我以后使用,请各位高手用正则实现,谢谢!ASP.NET(C#)中使用
...全文
1389 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2010-08-07
  • 打赏
  • 举报
回复
没想到当年那么菜…


private static void TestRegex03()
{
string html = @"<label id=""ArticleCnt"" runat=""server""><P align=center><FONT size=2><IMG alt=这是标题 src=""../pics/9943/9943444.jpg"" border=0 name=MM></FONT></P>
<DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center>
<P style=""TEXT-INDENT: 2em"">这是图片标题。 </P></DIV>
<P align=center><FONT size=2><IMG src=""../pics/9943/9943448.jpg"" border=0></FONT></P>
<DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center>图片标题</DIV>
<P align=center><FONT size=2><IMG src=""../pics/9943/9943455.jpg"" border=0></FONT></P>
</label>";
MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<img.*?src=""\.+/)[^"" ']+");
List<string> lines = new List<string>();
foreach (Match m in mc)
{
lines.Add(@"http://img1.qq.com/news/" + m.Value);
}
}
leixueqiyi 2008-04-23
  • 打赏
  • 举报
回复
4 楼 strife 的代码有点像为你量身定做的一样
hackztx 2008-04-23
  • 打赏
  • 举报
回复
我的代码你应用后出现什么问题!!
cisky 2008-04-23
  • 打赏
  • 举报
回复
抱歉各位,这几天比较忙,所以没上来看,今天各位的帮忙除了9楼没赶上以外其他都有仔细看过。
7 楼 yzlxy 和10 楼 hackztx 的方法可能用到我这个模块里面会有点问题,11楼用list<T>的方法也没有问题,特别是那个正则委托,没有接触过,现在长见识了,谢谢!

不过我可能没有表述清楚,我要实现的是将读取的内容修改相对地址存入我的一个LIST<T>中以供批量写入数据库,另外将连接的文件地址转化为绝对地址的数组,通过这个数组下载文件保存到本服务器指定目录。也就是说,修改后的内容中的连接会跟下载后保存的文件地址统一,以便显示文章的时候,图片和文件连接都是有效的,修改路径和下载的文件格式我也是通过input参数来决定的,以便于更具有针对性地下载某些文件到本地,对于不相干的连接,会通过人工编辑处理。

我先用用4 楼 strife 的方法,然后参照一下其他几位朋友的方法试试,这个跟我的想法很切近,谢谢!

e.g.:在获取A站资源的时候,我可能只需要下载图片,在获取B站资源的时候我可能还需要下载图片以外的文件如flash或者rar/zip,或者某个要抓取资源的站的代码非常规范,图片都是统一小写<img src="url.jpg" alt="" />格式,而有些却非常混乱。

再次感谢各位帮忙!
cisky 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 hackztx 的回复:]
我的代码你应用后出现什么问题!!
[/Quote]
没有问题,但是我不大习惯用string[]呵呵:)
.{1,20}定得有点死,因为我的规则是通过input设置的,设置规则的地方是由管理员输入一段HTML代码,然后用JS转化为正则规则的
root_ 2008-04-21
  • 打赏
  • 举报
回复
楼主所给需求有些不明确,所有图片地址都是相对地址吗,只有带有“../pics/”的才替换成“../images/”然后保存,还是所有的图片地址都保存?另外为什么不用泛型,而要用ArrayList呢,难不成楼主还在用1.1

写了一下,看看是不是你要的结果吧

方法一:用正则委托来做
using System.Collections;
using System.Text.RegularExpressions;
using System.Collections.Generic;

//ArrayList _arrayList;
List<string> list;
protected void Button1_Click(object sender, EventArgs e)
{
string testStr = @"<label id=""ArticleCnt"" runat=""server""><P align=center><FONT size=2><IMG alt=这是标题 src=""../pics/9943/9943444.jpg"" border=0 name=MM></FONT></P>
<DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center>
<P style=""TEXT-INDENT: 2em"">这是图片标题。 </P></DIV>
<P align=center><FONT size=2><IMG src=""../pics/9943/9943448.jpg"" border=0></FONT></P>
<DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center>图片标题</DIV>
<P align=center><FONT size=2><IMG src=""../pics/9943/9943455.jpg"" border=0></FONT></P>
</label>";

//_arrayList = new ArrayList();
list = new List<string>();
string result = Regex.Replace(testStr, @"(?<=<img[^>]*src="")[^""]*(?=""[^>]*>)", new MatchEvaluator(RegReplace), RegexOptions.IgnoreCase);

//foreach (object o in _arrayList)
// Response.Write(o.ToString() + "<br>");
foreach (string s in list)
Response.Write(s + "<br>");
}

private string RegReplace(Match m)
{
//_arrayList.Add(m.Value.Replace("../pics/", "http://img1.qq.com/news/images/"));
list.Add(m.Value.Replace("../pics/", "http://img1.qq.com/news/images/"));
return "";
}





方法二:直接匹配替换
using System.Collections;
using System.Text.RegularExpressions;
using System.Collections.Generic;

string testStr = @"<label id=""ArticleCnt"" runat=""server""><P align=center><FONT size=2><IMG alt=这是标题 src=""../pics/9943/9943444.jpg"" border=0 name=MM></FONT></P>
<DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center>
<P style=""TEXT-INDENT: 2em"">这是图片标题。 </P></DIV>
<P align=center><FONT size=2><IMG src=""../pics/9943/9943448.jpg"" border=0></FONT></P>
<DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center>图片标题</DIV>
<P align=center><FONT size=2><IMG src=""../pics/9943/9943455.jpg"" border=0></FONT></P>
</label>";

List<string> result = new List<string>();
MatchCollection mc = Regex.Matches(testStr, @"(?<=<img[^>]*src="")[^""]*(?=""[^>]*>)", RegexOptions.IgnoreCase);
foreach(Match m in mc)
result.Add(m.Value.Replace("../pics/", "http://img1.qq.com/news/images/"));

foreach (string s in result)
Response.Write(s + "<br>");
}




两种方法的输出都是
http://img1.qq.com/news/images/9943/9943444.jpg
http://img1.qq.com/news/images/9943/9943448.jpg
http://img1.qq.com/news/images/9943/9943455.jpg
至于哪种方法的效率高,要看你的数据源了,自己测一下吧
hackztx 2008-04-21
  • 打赏
  • 举报
回复
<IMG alt=这是标题 src="../pics/9943/9943444.jpg" border=0 na
将../pics/替换成../images///不明白为什么这样做,如果你非要这样做,请用replace方法来替换(string)

//ArrayList _arrayList = new ArrayList();
http://img1.qq.com/news/pics/9943/9943444.jpg
http://img1.qq.com/news/pics/9943/9943448.jpg
http://img1.qq.com/news/pics/9943/9943455.jpg


我不明白楼上的为什么要用match?明明是个循环!!

还有楼上的过客在我心目中应该是一个正则高手,我不明白他的回复为什么会被删除!!!

这明明是一个string[]就可以解决的问题,为什么非要arraylist,难道这个东西这么好用???

string[]和arraylist我都帮你写一个!!

using System;
using System.Text.RegularExpressions;
using System.Collections;
public class Test
{
static void Main(string[] args)
{
string input = @"<label id=""ArticleCnt"" runat=""server""><P align=center><FONT size=2><IMG alt=这是标题 src=""../pics/9943/9943444.jpg"" border=0 name=MM></FONT></P> <DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center> <P style=""TEXT-INDENT: 2em"">这是图片标题。 </P></DIV> <P align=center><FONT size=2><IMG src=""../pics/9943/9943448.jpg"" border=0></FONT></P> <DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center>图片标题</DIV> <P align=center><FONT size=2><IMG src=""../pics/9943/9943455.jpg"" border=0></FONT></P> </label>";
ArrayList arrLists;
string[] strArrs;
tsRegex(input,out arrLists,out strArrs);

foreach (string strArr in strArrs)
{
Console.WriteLine(strArr);
}

Console.WriteLine("\n");

foreach (string arrList in arrLists)
{
Console.WriteLine(arrList);
}
Console.ReadKey();

/*
http://img1.qq.com/news/pics/9943/9943444.jpg
http://img1.qq.com/news/pics/9943/9943448.jpg
http://img1.qq.com/news/pics/9943/9943455.jpg


http://img1.qq.com/news/pics/9943/9943444.jpg
http://img1.qq.com/news/pics/9943/9943448.jpg
http://img1.qq.com/news/pics/9943/9943455.jpg

*/
}

static void tsRegex(string input, out ArrayList arrLists, out string[] strArrs)
{
string strLink = "http://img1.qq.com/news/";
MatchCollection mc = Regex.Matches(input, @"<img.{1,20}src=[""']?(.*?)[""'\s>]",RegexOptions.IgnoreCase);
int count = mc.Count;
arrLists = new ArrayList();

if (count > 0)
{
strArrs = new string[count];
for (int i = 0; i < count; i++)
{
string strImage =strLink+ mc[i].Groups[1].Value.Replace("../","");
arrLists.Add(strImage);
strArrs[i] = strImage;
}
}
else
{
strArrs = new string[0];
}
}
}


yzlxy 2008-04-21
  • 打赏
  • 举报
回复
得到的是原始img 地址, 替换成你的路径很容易的,就不写了
yzlxy 2008-04-21
  • 打赏
  • 举报
回复
我写了一个, 其中htmlText参数Html文本


private ArrayList GetImages(string htmlText)
{
const string pattern = "<img [^~]*?>";
const string pattern1 ="src\\s*=\\s*((\"|\')?)(?<url>\\S+)(\"|\')?[^>]*";
ArrayList al = new ArrayList ();
Match match = Regex.Match(htmlText,pattern, RegexOptions.IgnoreCase); //找到img标记
while(match.Success)
{
string img = match.Value;
string imgsrc = Regex.Match(img, pattern1, RegexOptions.IgnoreCase).Result("${url}");
imgsrc = Regex.Replace(imgsrc, "\"|\'|\\>", "", RegexOptions.IgnoreCase);
al.Add(imgsrc );
match = match.NextMatch();

}

return al;

}
兔子-顾问 2008-04-20
  • 打赏
  • 举报
回复
(?<=<[iI][mM][gG].*? src=")(?:http)?[^"]+(?=")
strife013 2008-04-20
  • 打赏
  • 举报
回复
桌面有个id为div1的服务端控件,不知道合不合你的要求
strife013 2008-04-20
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class test正则 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = @"<P align=center><FONT size=2><IMG src=""../pics/9943/9943448.jpg"" border=0>
</FONT></P>
<DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center图片标题></DIV>
<P align=center><FONT size=2><IMG src=""../pics/9943/9943455.jpg"" border=0></FONT></P>";

string patten = "../pics";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(patten, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string str1 = rg.Replace(str, "../image");
div1.InnerText = str1;

string imghz = "jpg";
string patten1 = @"src=""(.*?)\." + imghz + @"""";
string head = @"""http://mysite";
Regex rg1 = new Regex(patten1);

ArrayList al = new ArrayList();
foreach (Match mt in rg1.Matches(str))
{
al.Add(head+mt.Value.Substring(8));
}

foreach (string st in al)
{
Response.Write(st + "<br>");
}
// Response.Write(str1);
}
}
strife013 2008-04-20
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class test正则 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = @"<P align=center><FONT size=2><IMG src=""../pics/9943/9943448.jpg"" border=0>
</FONT></P>
<DIV style=""FONT-SIZE: 10pt; FONT-FAMILY: 宋体"" align=center图片标题></DIV>
<P align=center><FONT size=2><IMG src=""../pics/9943/9943455.jpg"" border=0></FONT></P>";

string patten = "../pics";
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(patten, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string str1 = rg.Replace(str, "../image");
div1.InnerText = str1;

string imghz = "jpg";
string patten1 = @"src=""(.*?)\." + imghz + @"""";
Regex rg1 = new Regex(patten1);

ArrayList al = new ArrayList();
foreach (Match mt in rg1.Matches(str))
{
al.Add(mt.Value):
}
// Response.Write(str1);
}
}
cisky 2008-04-20
  • 打赏
  • 举报
回复
这个问题在之前已经有解决办法,但是效率实在低下,因为涉及到批量操作,所以想重新找一个更好的办法
之前的解决办法是,通过正则先找到所有的图片链接存入数组,并同时用regex.replace替换所有图片的链接地址,再对获取到的链接数组替换成绝对链接,可能是程序设计上有问题,或者是批量操作的文章太多了,所以速度很慢
叶子 2008-04-20
  • 打赏
  • 举报
回复
有难度……

110,500

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧