当前位置:编程学习 > C#/ASP.NET >>

正则替换 看看你是不是正则高手

抓取到的代码里  有这样一段html  导致导入到数据库里图片不显示

<img id="img_246" src="static/image/common/none.gif" zoomfile="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" file="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" class="zoom" /> 


也就是src属性 根本不是正确的图片, 导致我抓取到的不显示 ,而file属性有完整的图片路径  我现在想把src属性(static/image/common/none.gif)  替换为file属性 的(http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg)

想要的结果
<img id="img_246" src="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" zoomfile="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" file="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" class="zoom" />
--------------------编程问答--------------------

<script>
   var stertre = '<img id="img_246" src="static/image/common/none.gif" zoomfile="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" file="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" class="zoom" />';
   stertre = stertre.replace(/^<img(.*?)file="(.*?)" (.*?)\/>$/, '<img id="img_246" src="$2" zoomfile="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" file="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" class="zoom" />');
   alert(stertre);
</script>
--------------------编程问答-------------------- 如果你要匹配全部 就把正则换成

/^\<img(.*?)file="(.*?)" (.*?)\/>\$/ --------------------编程问答-------------------- 例如:

<script>
   var stertre = '<img id="img_246" src="static/image/common/none.gif" 

zoomfile="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" 

file="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" class="zoom" 

/><img id="img_246" src="static/image/common/none.gif" 

zoomfile="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" 

file="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" class="zoom" 

/><img id="img_246" src="static/image/common/none.gif" 

zoomfile="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" 

file="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" class="zoom" 

/>';
   stertre = stertre.replace(/^\<img(.*?)file="(.*?)" (.*?)\/>\$/, '<img id="img_246" 

src="$2" zoomfile="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" 

file="http://bbsnew.pic.aaa.com/pic/1121/11210213/month_1309/bbb.jpg" class="zoom" 

/>');
   alert(stertre);
</script>
--------------------编程问答--------------------  亲  发到.net的版块是想要 C#语言的   在看看亲 --------------------编程问答-------------------- 直接文件本替换就可以啦
src="static/image/common/none.gif" 替换为空字符
file="http:// 替换为 src="http:// --------------------编程问答-------------------- yourhtml=Regex.Replace(yourhtml,@"(?is)(?<=<img\b[^>]*?src=(['""]?))(.*?)(?=\1[^>]*?\bfile=\1(.*?)\1[^>]*?>)","$3"); --------------------编程问答--------------------

string str = "原来的字符串";//(<img id="img_246" src="static/image/common/none.gif"……)
string fileStr = Regex.Match(str, "(?<=\\sfile=[\"']).*?(?=[\"'])").ToString();
string newStr = Regex.Replace(str, "(?<=src=[\"']).*?(?=[\"'])", fileStr);
//这个newStr 就是你要的字符串
--------------------编程问答--------------------
引用 4 楼 w87875251l 的回复:
 亲  发到.net的版块是想要 C#语言的   在看看亲


我突然看LZ您好不爽  JS正则与C#的差多少 增加动手啊 

 string str = "<img id=\"img_246\" src=\"gif\" zoomfile=\"jpg\" file=\"jpg\" class=\"zoom\" /><img id=\"img_246\" src=\"gif\" zoomfile=\"jpg\" file=\"jpg\" class=\"zoom\" />";
      string newstr= Regex.Replace(str,"<img(.*?)file=\"(.*?)\" (.*?)/>","<img id=\"img_246\" src=\"$2\" zoomfile=\"jpg\" file=\"jpg\" class=\"zoom\" />");
--------------------编程问答-------------------- 自己动手 把JS变成C#的 分分钟的事 --------------------编程问答-------------------- Regex regImg = 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); --------------------编程问答-------------------- 楼主问问题就老老实实问问题。搞得像是别人在接受你的测试一样。 --------------------编程问答-------------------- 其实很简单的问题replace(s,"src=\"","src=\"http://bbsnew.pic.aaa.com/pic" --------------------编程问答--------------------
引用 11 楼 systemx 的回复:
楼主问问题就老老实实问问题。搞得像是别人在接受你的测试一样。

我很鄙视这种人 --------------------编程问答-------------------- regex.replace(html,"src=(...) file=(....)","src=${2}") --------------------编程问答-------------------- 正则都拿到手自己改改不就成了。 --------------------编程问答-------------------- --------------------编程问答-------------------- 正则貌似只能匹配
你可以匹配后替换
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,