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

  • Home
  • SEARCH
  • 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 4321972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T08:48:51+00:00 2026-05-21T08:48:51+00:00

Basically what I would like to do is run multiple (15-25) regex replaces on

  • 0

Basically what I would like to do is run multiple (15-25) regex replaces on a single string with the best possible memory management.

Overview:
Streams a text only file (sometimes html) via ftp appending to a StringBuilder to get a very large string. The file size ranges from 300KB to 30MB.

The regular expressions are semi-complex, but require multiple lines of the file (identifying sections of a book for example), so arbitrarily breaking the string, or running the replace on every download loop is out of the answer.

A sample replace:

Regex re = new Regex("<A.*?>Table of Contents</A>", RegexOptions.IgnoreCase);
source = re.Replace(source, "");

With each run of a replace the memory sky rockets, I know this is because string are immutable in C# and it needs to make a copy – even if I call GC.Collect() it still doesn’t help enough for a 30MB file.

Any advice on a better way to approach, or a way to perform multiple regex replaces using constant memory (make 2 copies (so 60MB in memory), perform search, discard copy back to 30MB)?

Update:

There does not appear to be a simple answer but for future people looking at this I ended up using a combination of all the answers below to get it to an acceptable state:

  1. If possible split the string into chunks, see manojlds’s answer for a way to that as the file is being read – looking for suitable end points.

  2. If you can’t split as it streams, at least split it later if possible – see ChrisWue’s answer for some external tools that may help with this process to piping to files.

  3. Optimize the regex, avoid greedy operators and try to limit what the engine has to do as much as possible – see Sylverdrag’s answer.

  4. Combine the regex when possible, this cuts down the number of replaces for when the regexs are not based on each other (useful in this case for cleaning bad input) – see Brian Reichle’s answer for a code sample.

Thank you all!

  • 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-21T08:48:52+00:00Added an answer on May 21, 2026 at 8:48 am

    Depending on the nature of the RegEx’s, you might be able to combine them into a single regular expression and use the overload of Replace() that takes in a MatchEvaluator delegate to determine the replacement from the matched string.

    Regex re = new Regex("First Pattern|Second Pattern|Super(Mega)*Delux", RegexOptions.IgnoreCase);
    
    source = re.Replace(source, delegate(Match m)
    {
        string value = m.Value;
    
        if(value.Equals("first pattern", StringComparison.OrdinalIgnoreCase)
        {
            return "1st";
        }
        else if(value.Equals("second pattern", StringComparison.OrdinalIgnoreCase)
        {
            return "2nd";
        }
        else
        {
            return "";
        }
    });
    

    Of course this falls apart if latter patterns need to be able to match on the result of earlier replacements.

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

Sidebar

Related Questions

I would like to automatically update my DNS more multiple domains programmatically. I run
Basically I would like to store the address of a pointer in a buffer.
Basically I would like to tell MSTest to execute a bit of code before
Having another tough one here guys, basically I would like to check if a
I would like to basically get all the events that i get invited to
I would like to create a custom data type which basically behaves like an
I would like to set a variable in bash called test_var Basically, I want
I would like to add a preprocessor to HTML pages. Basically, I have a
I would like to have fuzzy looking border around my Canvas control. Basically, I
Basically, I would like to decode a given HTML document, and replace all special

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.