revolutionkpi
revolutionkpi

Reputation: 2682

How correctly replace string

In my code I find all matches elements and replace it with special values.

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

But, my code is wrong because if there is more than one match it replace only the last one, how can I correct my code for replace all matches in text?

Upvotes: 2

Views: 173

Answers (4)

Mark Byers
Mark Byers

Reputation: 838196

You are missing curly braces around the body of your for loop. Without the curly braces the only line that gets executed multiple times is the first one.

Try this instead:

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

I would also like to add two further points:

  • There is a method called Regex.Replace that you can use instead of first finding the strings you want to replace using a regex, and then using string.Replace.
  • If you are trying to parse HTML it would be better to use a HTML parser. Take a look at HTML Agility Pack to see if it could be an easier way to solve your problem.

Upvotes: 3

Rohan Büchner
Rohan Büchner

Reputation: 5393

I think you might be missing a set of brackets around your loop...

Only this line gets looped. That's why your code only updates the last entry, as result is set to the last item in the collection (on the last iteration of the foreach)

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

Corrected code

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

Upvotes: 1

AwesomeTown
AwesomeTown

Reputation: 2880

Would the Regex.Replace method not simplify what you're trying to accomplish?

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

Upvotes: 0

Ras
Ras

Reputation: 628

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

Upvotes: 1

Related Questions