gpt4 book ai didi

c# - 正则表达式替换和字符串不变性

转载 作者:太空宇宙 更新时间:2023-11-03 17:58:17 27 4
gpt4 key购买 nike

说我有一个编译的Regex对象:

    public static Regex myRgx = new Regex(@"[\d]+",RegexOptions.Compiled); 

Now let's say I'm reading large strings into the string variable SS , And then I use my Regex object to replace all matches within that string

    myRgx.Replace(SS,"($&)");

Question: Does .Replace internally use a StringBuilder to do the work , much like what happens in String.ReplaceAll() ?
And if it doesn't is there a way to get around this ?
Update :
I don't know if it's ok to ask another question as an update to the original question .. feel free to edit this out if it's not ok.
Question 2 : What if I need to preform a chain of replacements , Using multiple Regex objects, As in:

string input = "Some LARGE string";
input = rgx1.Replace(input,"substitution1");
input = rgx2.Replace(input,"substitution2");
input = rgx3.Replace(input,"substitution3");


我正在编写一个形态分析器,因此需要将正则表达式对象保持分开,并且替换必须按照上述代码中的特定顺序进行。正则表达式对象的数量很大,我们谈论的是千兆字节的文本,因此每次传递完一个正则表达式对象后,都要传递一个新的字符串对象,这实际上不是一个选择。
有什么建议么?

最佳答案

是的,Regex.Replace方法使用通过反射器发现的StringBuilder

关于c# - 正则表达式替换和字符串不变性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5720708/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com