现在的位置: 首页 > 综合 > 正文

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

2013年10月12日 ⁄ 综合 ⁄ 共 1205字 ⁄ 字号 评论关闭

问题:http://community.csdn.net/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>");

抱歉!评论已关闭.