C# 求个正则表达式 过滤文章img中src除外意外的所有属性

<img alt="描述描述描述描述" img_height="316" img_width="591" inline="0" src="这个保留" />
<img style="width:100px; height:100px;" src="这个保留" />
<img height="316" width="591" src="这个保留" />
过滤后
<img src="这个保留" />

按照你的要求写的C#正则表达式 (?is)(<img ).*?(src=\".*?\").*?(/>) 替换为 $1$2$3

完整的C#过滤文章img中除src以外所有属性的程序如下

using System;
using System.Text.RegularExpressions;
namespace retainsrc{
 class RetainSRC{
  static void Main(string[] args){
   string str="<img alt=\"描述描述描述描述\" img_height=\"316\" img_width=\"591\" inline=\"0\" src=\"这个保留\" /><img style=\"width:100px;height:100px;\" src=\"这个保留\" /><img height=\"316\" width=\"591\" src=\"这个保留\" />";
   string pattern = "(?is)(<img ).*?(src=\".*?\").*?(/>)";
   Regex rgx = new Regex(pattern);
   string result=rgx.Replace(str,"$1$2$3");
   Console.WriteLine(result);
   Console.ReadKey();
  }
 }
}


运行结果
<img src="这个保留"/><img src="这个保留"/><img src="这个保留"/>

温馨提示:答案为网友推荐,仅供参考
相似回答