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 500849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:07:45+00:00 2026-05-13T06:07:45+00:00

I need to find in a text everything that starts with [ and ends

  • 0

I need to find in a text everything that starts with [ and ends with ] and replace it with the value that a function returns. So, here is an example of what I’m doing:

    public string ProcessTemplate(string input)
    {
        return Regex.Replace(input, @"\[(.*?)\]", new MatchEvaluator(delegate(Match match)
            {
                return ProcessBlock(match.Result("$1"));
            }));
    }

    public string ProcessBlock(string value)
    {
        return Block.FromString(value).Process();
    }

Now, my problem is when I need to edit blocks. So I thought to find blocks, edit them, and then replacing them in the text.

So, I created a List of blocks, and separated the ProcessTemplate method to two methods: FindBlocks and ReplaceBlocks:

    public void FindBlocks(string input)
    {
        Input = input;

        foreach (Match match in Regex.Matches(input, @"\[(.*?)\]"))
            Blocks.Add(Block.FromString(match.Result("$1")));
    }

    public string ReplaceBlocks()
    {
        string input = Input;

        foreach (Block block in Blocks)
            input = input.Replace("[" + block.OrginalText + "]", block.Process);

        return input;
    }

    public IList<Block> Blocks
    {
        get;
        set;
    }

    public string Input
    {
        get;
        set;
    }

It’s working, but the problem is that it is pretty slow. I measured with System.Diagnostics.Stopwatch every part, and I found out that the String.Replace in the ReplaceBlocks method is pretty slow.

How can I improve it?

Thanks.

  • 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-05-13T06:07:46+00:00Added an answer on May 13, 2026 at 6:07 am

    replacing the string in ReplaceBlock with a StringBuilder may provide a performance increase as every time you perform a string.replace it will have to deallocate the string and reallocate the string. string builder doesnt need to do this.

    Replace the contents of the ReplaceBlock with the following.

    // This will require a reference to System.Text
    StringBuilder input =new StringBuilder(Input);
      foreach (Block block in Blocks)
      {
        input = input.Replace("[" + block.OrginalText + "]", block.Process);
      }
      return input.ToString();
    

    I have also found replacing the foreach loops with a for loop which are quicker.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to find occurrences of ~ 25 000 words within a text. What
I need to find/create an application that will create employee web usage reports from
I need to find out the time a function takes for computing the performance
I need to find all occurrences of a function call in a C++ file
I need to find out how to format numbers as strings. My code is
I need to find a bottleneck and need to accurately as possible measure time.
I need to find the PID of the current running process on a Linux
I need to find occurrences of (+) in my sql scripts, (i.e., Oracle outer
I need to find a way to crawl one of our company's web applications
I need to find out what ports are attached to which processes on a

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.