正则表达式提取URL中的文件名和替换

本文介绍了一种使用正则表达式从HTML代码中提取图片链接并替换为带有超链接的<img>标签的方法。通过示例代码展示了如何实现这一功能。

问题:http://community.youkuaiyun.com/Expert/topic/4706/4706491.xml?temp=6.908596E-03
在FreeTexBox中的所以html内容中找到图片html语言(<IMG height=550 src="http://localhost:2045/glietnet/image/news/news20060414105529.jpg" width=412 border=0>)或这句语言中的图片名字(news20060414105529.jpg)?
  如果可以的话能否把这句图片html替换成<A href="image/news/news20060414105529.jpg"><IMG _fcksavedurl=""image/news/news20060414105529.jpg"><IMG" height=550 src="http://localhost:2045/glietnet/image/news/news20060414105529.jpg" width=412 border=0></A> 

主要是提取http://localhost:2045/glietnet/image/news/news20060414105529.jpg中的文件名
实现方法如下:
string temp="http://localhost:2045/glietnet/image/news/news20060414105529.jpg";
temp=Regex.Match(temp,"[^/]//w*//.//w*",RegexOptions.IgnoreCase).ToString();
Response.Write("<script>alert('"+temp+"')</script>");

完整的实现替换如下:
string temp="http://localhost:2045/glietnet/image/news/news20060414105529.jpg";
string temp1="<IMG height=550 src=http://localhost:2045/glietnet/image/news/news20060414105529.jpg width=412 border=0>";

 temp=Regex.Match(temp,"[^/]//w*//.//w*",RegexOptions.IgnoreCase).ToString();
 temp1=Regex.Replace(temp1,@"<img .*>",@"<a href=image//news//"+temp+">"+temp1+"</a>",RegexOptions.IgnoreCase);
Response.Write("<script>alert('"+temp1+"')</script>");

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值