在 StringBuilder 上替换正则表达式的最佳方法是什么?
可能的重复:
StringBuilder 中的正则表达式替换
多次执行正则表达式替换的最佳方法是什么,在 StringBuilder 上?
如果您不介意成为 tl;dr
人,请进一步阅读以了解详细信息:
嗨,我有一个函数可以对字符串进行大量字符串操作。所以很自然地,我使用 StringBuilder 类来实现它。现在我陷入了两难的境地。
我的功能是这样的:
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"))
现在,我有一个..我想要替换的自定义单词(模式)列表 - 大约20个模式..
我试图用它们各自的图像替换所有笑脸符号;像这样:
:) becomes <img src="smile.png" />
;) becomes <img src="wink.png" />
等等...我有大约 20 个图像/符号要替换,我正在使用这个正则表达式
(?<=^|\s):d(?=$|\s) //positive lookahead and lookback at :d
Bob Vale 善意提供。
所有这一切都很棒,除了,我不知道如何用 StringBuilder 替换正则表达式,而且我不想像这样创建一个新的 StringBuilder:
ParsedText = new StringBuilder(Regex.Replace(...));
二十次,因为我认为它违背了整个内存保护目的。
那么,在 StringBuilder 上进行正则表达式替换的最佳方法是什么?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是将现有过滤器重构为可以调用以立即运行所有过滤器的方法。完成此操作后,您可以更改代码,以便每次在添加新字符串之前附加到字符串构建器时开始为较小的字符串调用此方法,而不是等到最后才多次构建大字符串。这很重要,因为它可以让您避免遇到大对象堆的问题,并且对垃圾收集器更加友好。
一旦你做到了这一点,如果你真的雄心勃勃,你也可以重写以开始使用流而不是字符串构建器。这将允许您将多个过滤器组合成一个定制的、高效的状态机,这应该对性能产生可衡量的积极影响。但这最后一步将以代码清晰度和易于维护为代价,因此除非您认为此代码可以提高应用程序的性能,否则不要这样做。
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.
请参阅此问题和Jon Skeet 的建议:
See this question and advice from Jon Skeet: