我试图替换以下HTML中每张图片的src
属性:
var html = @"<img height=49 src=""Image25.gif"" width=94> or <img height=44 src=""Image26.gif"" width=117> or <img height=41 src=""Image27.gif"" width=46>";
Console.WriteLine( Regex.Replace( html,
@"(<img.*src="")(.*"".*>)",
m => m.Groups[1] + "/File/Download/" + m.Groups[2],
RegexOptions.IgnoreCase ) );
其输出为:
<img height=49 src="Image25.gif" width=94> or <img height=44 src="Image26.gif" width=117> or <img height=41 src="/File/Download/Image27.gif" width=46>
输出应为:
<img height=49 src="/File/Download/Image25.gif" width=94> or <img height=44 src="/File/Download/Image26.gif" width=117> or <img height=41 src="/File/Download/Image27.gif" width=46>
你可以使用C#在这样的单行上进行多次替换吗?
答案 0 :(得分:2)
尝试*?
懒惰quantifiers:
Console.WriteLine(Regex.Replace(html,
@"(<img.*?src="")(.*?"".*?>)",
m => m.Groups[1] + "/File/Download/" + m.Groups[2],
RegexOptions.IgnoreCase));