我需要做字符串替换...我需要处理的只有少数病例:任何改善Regex.Replace性能的方法?
1) optional case insensitive
2) optional whole words
现在,我使用_myRegEx.Replace()...如果指定了#1,我添加RegexOptions.IgnoreCase标志。如果指定了#2,则将搜索词包装在\ b <字> \ b中。
这工作正常,但它真的很慢。我的基准测试需要1100ms,而使用String.Replace测试需要90ms。显然,一些问题这样做:
1) case insensitive is tricky
2) regex \b<word>\b will handle "<word>", " <word>", "<word> " and " <word> "... string replace would only handle " <word> ".
我已经使用了RegexOptions.Compiled标志。
还有其他的选择吗?
@ M.kazemAkhgary,我没有使用静态,我正在使用实例...我已更新帖子以澄清。 – SledgeHammer
你如何实例化'Regex'实例?有一些正则表达式模式,因为它是已知的缓慢[像这样](http://stackoverflow.com/questions/9687596/slow-regex-performance),甚至微软给出了[使用正则表达式的最佳实践](https:/ /msdn.microsoft.com/en-us/library/gg578045(v=vs.110).aspx)。 –
@BagusTesa只是像新的正则表达式(“\ bTest \ b”,RegexOptions.Compiled | RegexOptions.IgnoreCase);对于案例1和2. – SledgeHammer