私のコードでは、一致するすべての要素を見つけて、それを特別な値に置き換えます。
Regex imgRule = new Regex("img id=\\\".+?\\\"");
MatchCollection matches = imgRule.Matches(content.Value);
string result = null;
foreach (Match match in matches)
result = match.Value;
if (result != null)
{
var firstOrDefault = node.ListImages.FirstOrDefault();
if (firstOrDefault != null)
{
var htmlWithImages = content.Value.Replace(result, string.Format("img src='{0}' class='newsimage' width='300'", firstOrDefault.ImageUrlId));
node.Content = htmlWithImages;
}
}
しかし、複数の一致がある場合、最後の一致のみを置き換えるため、私のコードは間違っています。テキスト内のすべての一致を置き換えるためにコードを修正するにはどうすればよいですか?