Possible duplicate:
Replace regular expressions inside StringBuilder
What is the best way to reuse Regex Replace on a StringBuilder?
If you do not mind being a tl;dr person, read on for details:
Hi, I have a function that performs quite a lot of string operations on a string. Therefore, naturally, for this I use the StringBuilder class. Now I'm in a pretty dilemma.
My function looks 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 like to replace - about 20 patterns.
I am trying to replace all emoticon characters with appropriate images; So:
:) becomes <img src="smile.png" /> ;) becomes <img src="wink.png" />
etc. I have about 20 images / characters to replace, and I use this regex
(?<=^|\s):d(?=$|\s) //positive lookahead and lookback at :d
which Bob Vale is kindly provided.
All this is great, except that I do not know how to replace the regular expression with StringBuilder, and I do not want to create a new StringBuilder as follows:
ParsedText = new StringBuilder(Regex.Replace(...));
twenty times since I think he defeats the whole purpose of preserving memory.
So, What is the best way to make a Regex Replace on a StringBuilder?
Thanks!