I have a large string of asp code and I want to modify some parts of it using regex. I have a pattern and want to replace all instances of it with html comments wrapped around it. I have this so far
foreach (Match controlMatch in Regex.Matches(bodyText, "<asp:Image.*?\\/>", RegexOptions.IgnoreCase | RegexOptions.Singleline))
{
bodyText = bodyText.Replace(controlMatch.Groups[0].Value, "<!--" + controlMatch.Groups[0].Value + "-->");
}
But the problem is, when I call replace, it replaces all the other instances that I already wrapped in html comments and it ends up looking like
<!--<!--<!--<!--<asp:Image ... /> -->-->-->-->
Does anyone know how to fix this? Coincidentally the matches of the pattern happen to be the same exact string which is why this happens, but in general it can be different.