一个StringBuilder内正则表达式替换 [英] Regex replacements inside a StringBuilder

查看:756
本文介绍了一个StringBuilder内正则表达式替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个文本文件的内容一个StringBuilder,然后我要执行大量查找/使用常规的前pressions包含在StringBuilder的文本替换操作。

I'm writing the contents of a text file to a StringBuilder and I then want to perform a number of find/replace actions on the text contained in the StringBuilder using regular expressions.

我碰到的StringBuilder的替换功能的一个问题是不能够接受正规的前pression论点。

I've run into a problem as the StringBuilder replace function is not capable of accepting regular expression arguments.

我可以使用Regex.Replace上的正常字符串,但我的即时pression下林,这是低效率的,由于这一事实,即在字符串的两个副本将需要在内存中创建为达字符串是不可改变的。

I could use Regex.Replace on a normal string but I'm under the impression that this is inefficient due to the fact that two copies of the string will need to be created in memory as .net strings are immutable.

在我已经更新我打算把它写回原来的文件中的文本。

Once I've updated the text I plan to write it back to the original file.

什么是解决我的问题的最佳和最有效的方法是什么?

What's the best and most efficient way to solve my problem?

修改

除了答案(S)以下时,我发现,也流下了我的问题,一些轻以下问题 -

In addition to the answer(s) below, I've found the following questions that also shed some light on my problem -

推荐答案

最好的和最有效的解决方案的您的时间的是,首先尝试最简单的方法:忘了的StringBuilder ,只使用 Regex.Replace 。然后找出它是多么慢 - 这很可能是不够好。不要忘了尝试在这两个编译和非编译模式正则表达式。

The best and most efficient solution for your time is to try the simplest approach first: forget the StringBuilder and just use Regex.Replace. Then find out how slow it is - it may very well be good enough. Don't forget to try the regex in both compiled and non-compiled mode.

如果说的的速度不够快,可以考虑使用的StringBuilder 任何替代品,你可以前preSS简单,然后用 Regex.Replace 为休息。您可能还需要考虑尝试替换结合,减少使用正则表达式(因此中间字符串)的数量。

If that isn't fast enough, consider using a StringBuilder for any replacements you can express simply, and then use Regex.Replace for the rest. You might also want to consider trying to combine replacements, reducing the number of regexes (and thus intermediate strings) used.

这篇关于一个StringBuilder内正则表达式替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆