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,请提供。

所有这些都很好,除了,我不知道如何用StringBuilder来正则表达式替换,而且我不想像这样创建一个新的StringBuilder:

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

我认为它超过了整个内存保护目的的20倍。

那么,在StringBuilder上执行正则表达式替换的最佳方法是什么?

谢谢!

最佳答案

最简单的方法是将现有过滤器重构为一种可以调用以同时运行所有过滤器的方法。完成此操作后,您可以更改代码,以便在每次添加新字符串之前将其追加到stringbuilder时,都针对较小的字符串开始调用此方法,而不必等到最后并不得不多次构建较大的字符串。这很重要,因为它可以使您免于以后遇到大对象堆的问题,否则对垃圾回收器更加友好。

达到目标后,如果您真的很雄心勃勃,还可以重新编写以开始使用流,而不是使用stringbuilder。这样一来,您就可以将许多过滤器组合到自定义的高效状态机中,从而对性能产生可观的积极影响。但是,这最后一步将以代码清晰和易于维护为代价,因此,除非您将这段代码视为插入应用程序性能的驱动器,否则请不要这样做。

关于c# - 在StringBuilder上替换正则表达式的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6791557/

10-14 11:48