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

w87875251l 2013-09-25 05:42:20
抓取到的代码里 有这样一段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" />
...全文
694 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaoshiyao 2013-09-27
  • 打赏
  • 举报
回复
正则貌似只能匹配 你可以匹配后替换
c02645 2013-09-26
  • 打赏
  • 举报
回复
其实很简单的问题replace(s,"src=\"","src=\"http://bbsnew.pic.aaa.com/pic"
  • 打赏
  • 举报
回复
楼主问问题就老老实实问问题。搞得像是别人在接受你的测试一样。
我是地摊小贩 2013-09-26
  • 打赏
  • 举报
回复
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);
「已注销」 2013-09-26
  • 打赏
  • 举报
回复
自己动手 把JS变成C#的 分分钟的事
「已注销」 2013-09-26
  • 打赏
  • 举报
回复
引用 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\" />");
bxyyyyyyyyy 2013-09-26
  • 打赏
  • 举报
回复
xiye_jfb 2013-09-26
  • 打赏
  • 举报
回复

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 就是你要的字符串
q107770540 2013-09-26
  • 打赏
  • 举报
回复
yourhtml=Regex.Replace(yourhtml,@"(?is)(?<=<img\b[^>]*?src=(['""]?))(.*?)(?=\1[^>]*?\bfile=\1(.*?)\1[^>]*?>)","$3");
WM_JAWIN 2013-09-26
  • 打赏
  • 举报
回复
直接文件本替换就可以啦 src="static/image/common/none.gif" 替换为空字符 file="http:// 替换为 src="http://
w87875251l 2013-09-26
  • 打赏
  • 举报
回复
亲 发到.net的版块是想要 C#语言的 在看看亲
  • 打赏
  • 举报
回复
正则都拿到手自己改改不就成了。
blackant2 2013-09-26
  • 打赏
  • 举报
回复
regex.replace(html,"src=(...) file=(....)","src=${2}")
「已注销」 2013-09-26
  • 打赏
  • 举报
回复
引用 11 楼 systemx 的回复:
楼主问问题就老老实实问问题。搞得像是别人在接受你的测试一样。
我很鄙视这种人
「已注销」 2013-09-25
  • 打赏
  • 举报
回复
例如:

<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>
「已注销」 2013-09-25
  • 打赏
  • 举报
回复
如果你要匹配全部 就把正则换成 /^\<img(.*?)file="(.*?)" (.*?)\/>\$/
「已注销」 2013-09-25
  • 打赏
  • 举报
回复

<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>

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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