2

私のコードでは、一致するすべての要素を見つけて、それを特別な値に置き換えます。

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

しかし、複数の一致がある場合、最後の一致のみを置き換えるため、私のコードは間違っています。テキスト内のすべての一致を置き換えるためにコードを修正するにはどうすればよいですか?

4

4 に答える 4

3

for ループの本体に中かっこがありません。中括弧がなければ、複数回実行される唯一の行は最初の行です。

代わりにこれを試してください:

foreach (Match match in matches)
{                                    // added curly brace here
    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;
        }
    }
}                                    // added curly brace here

また、さらに次の 2 点を追加したいと思います。

  • 最初にRegex.Replace正規表現を使用して置換する文字列を見つけてからstring.Replace.
  • HTML を解析しようとしている場合は、HTML パーサーを使用することをお勧めします。HTML Agility Packを見て、問題を解決するためのより簡単な方法であるかどうかを確認してください。
于 2012-05-28T13:51:10.420 に答える
1

ループの周りに一連のブラケットが欠けている可能性があると思います...

この行だけがループします。結果がコレクション内の最後の項目に設定されるため (foreach の最後の反復で)、コードが最後のエントリのみを更新するのはそのためです。

         foreach (Match match in matches) 
                      result = match.Value;

修正コード

  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;
                               }
                           }   
                        }
于 2012-05-28T13:51:09.700 に答える
1
foreach (マッチでマッチ)
{
    結果 = match.Value;

    if (結果 != 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;
        }
    }
}
于 2012-05-28T13:51:40.150 に答える
0

Regex.Replace メソッドは、達成しようとしていることを単純化しませんか?

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=vs.71).aspx

于 2012-05-28T13:52:19.233 に答える