正则表达式替换img标签src值!!!

方法一:

相关链接:http://bbs.csdn.net/topics/320185735

实例:此实例自己做的时候讲字符串加了alt进行了有关修改  不清楚看上面链接

string test = "<IMG alt="" src=\"/upload/2009_11/09112110144808.jpg\" border=0>";

string outhost = "www.myname.com";

Regex reg = new Regex(@"(?is)(?<=<img\b[^>]*?src=mg\s+src=(['""]?))(?!http://)(?=[^'""\s>]+\1)");

string result = reg.Replace(test, "http://" + outhost );

/*----------输出------------

<IMG src="http://www.myname.com/upload/2009_11/09112110144808.jpg" border=0>

*/

方法二:

相关链接:http://bbs.csdn.net/topics/390866982

public static string ReplaceImgPath(string intro)
        {
            string imgPath = string.IsNullOrEmpty(PubConfig.SportQuanWeb) ? "/" : PubConfig.SportQuanWeb;
            Regex reg = new Regex(@"(?i)(?<=<img\b[^>]*?src=\s*(['""]?))([^'""]*/)+(?=[^'""/]+\1)");
            return reg.Replace(intro, imgPath);
        }

如上面代码只实现替换文件名前面的src,与实际效果不符合;

实际要求:

字符串infro包含很多img标签,例如<img alt="" src="/UserFiles/files/120CAOKY6QZ.jpg" style="width: 120px; height: 120px;" />

调用此方法后所有的img标签应该是这样的<img alt="" src="http://198.168.1.2/UserFiles
/files/120CAOKY6QZ.jpg" style="width: 120px; height: 120px;" />

 
实现方案:
str=Regex.Replace(str,@"(?i)(?<=<img\b[^>]*?src=(['""]?))[^'""]+(?=\1)","http://198.168.1.2$0");
上一篇:解决SQL Server管理器无法连接远程数据库Error: 1326错误


下一篇:SAMBA服务和FTP服务讲解(week3_day1)--技术流ken