Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to Regex replace on StringBuilder? [duplicate]

Possible Duplicate:
Regex replacements inside a StringBuilder

What is the best way to do a Regex Replace, many times, on StringBuilder?

If you don't mind NOT being a tl;dr person, read further for details:

Hi, I have a function that does quite a lot of string manipulations on a string. So naturally, I am using StringBuilder class for it. Now I am in quite a dilemma.

My function is something like this:

 ParsedText.Append("some footers here");
 ParsedText.Replace("[b]","<b>"); //format all bold opens
 ParsedText.Replace("[/b]","</b>"); //format all bold closes
 ParsedText.Replace("\n","<br />"); //format newlines

 .... sh!* load of other replaces and manipulations ...

 //Add <a href> to all links
 ParsedText = new StringBuilder(Regex.Replace(ParsedText, "pattern", "replacement"))

And now, I have a.. custom list of words (patterns) that I would want to replace - about 20 patterns..

I am trying to replace all smiley symbols with their respective images; like so:

:) becomes <img src="smile.png" />
;) becomes <img src="wink.png" />

and etc...I have about 20 images / symbols to replace and I am using this regex

(?<=^|\s):d(?=$|\s) //positive lookahead and lookback at :d

which Bob Vale kindly provided.

All this is great, except, I don't know how to regex replace with StringBuilder and I don't want to create a new StringBuilder like so:

 ParsedText = new StringBuilder(Regex.Replace(...));

twenty times as I think it defeats the whole memory conservation purpose.

So, What is the best way to do a Regex Replace on StringBuilder?

Thanks!

like image 510
LocustHorde Avatar asked Jul 22 '11 14:07

LocustHorde


2 Answers

The simplest way to do this is to refactor your existing filters out to a method you can call to run all the filters at once. Once that is done, you can change the code to start calling this method for the smaller string every time you append to the stringbuilder before the new string is added, rather than waiting until the end and having to build the large string multiple times. This is important because it can save you from running into problems with Large Object Heap down the road, and is otherwise a lot more garbage-collector friendly.

Once you get that far, if you're really ambitious you can also re-write to start using streams rather than a stringbuilder. That will allow you to combine a number of your filters into a custom, highly-efficient state machine that should have a measurable positive impact on performance. But this last step will come at the cost of code clarity and easy maintenance, so don't do it unless you see this code as driving the performance of your app.

like image 162
Joel Coehoorn Avatar answered Oct 27 '22 01:10

Joel Coehoorn


See this question and advice from Jon Skeet:

The best and most efficient solution for your time is to try the simplest approach first: forget the StringBuilder and just use Regex.Replace.

like image 27
VMAtm Avatar answered Oct 27 '22 01:10

VMAtm