Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8020241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:37:57+00:00 2026-06-04T21:37:57+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:37:59+00:00Added an answer on June 4, 2026 at 9:37 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have borrowed code from this link PHP regex templating - find all occurrences
I am currently writing code in C++ to find all possible permutations of 6
I searched all over net for an hour i couldn't find a decent code
I'm trying to find a way to delay all code that takes place after
enter code hereHiya, so far I didn't use JSP at all and I find
I want to create and then destroy buttons, all with code, but can't find
What I'm trying to do is find all matches in a string, then wrap
I need to find all block elements in a given node. Block elements are
I have some code like this to find all instances of the template in
This is a snippet of my code. soup=BeautifulSoup(html_document) tabulka=soup.find(table,width=100%) dls=tabulka.findAll(dl,{class:resultClassify}) tps=tabulka.findAll(div,{class:pageT clearfix}) return render_to_response('result.html',{'search_key':search_key,'turnpages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.