如果不需要替换任何内容,C#/.NET 2.0 RegEx.Replace 会做什么?
当调用 RegEx.Replace 来处理大量字符串时,我有点担心性能问题,因为我不知道如果没有匹配项它会做什么。
public static void ReplaceOldWithNew(ref string input)
{
string pattern = //something here
input = Regex.Replace(input, pattern, FormatReplacement);
}
private string FormatReplacement(Match m)
{
return String.Concat("x", formatCount++);
}
我应该这样处理吗
public static void ReplaceOldWithNew(ref string input)
{
string pattern = //something here
if (RegEx.IsMatch(input, pattern))
input = Regex.Replace(input, pattern, FormatReplacement);
}
?问题是,如果存在匹配项,它会搜索输入字符串两次。是否有任何解决方案仅搜索一次匹配项并仅在需要时创建新的字符串实例。也许使用 RegEx.Matches 或其他东西。
谢谢& BR-马蒂
I'm a bit worried about performance issue when calling RegEx.Replace to really big amount of strings since i have no idea what it does if there's no matches.
public static void ReplaceOldWithNew(ref string input)
{
string pattern = //something here
input = Regex.Replace(input, pattern, FormatReplacement);
}
private string FormatReplacement(Match m)
{
return String.Concat("x", formatCount++);
}
should I have it like this
public static void ReplaceOldWithNew(ref string input)
{
string pattern = //something here
if (RegEx.IsMatch(input, pattern))
input = Regex.Replace(input, pattern, FormatReplacement);
}
the problem with this is that it searches the input string two times if there is match(es). Is there any solution that would search the matches only once and make new string instance only if needed. Maybe using RegEx.Matches or something.
Thanks & BR - Matti
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 Reflector 检查
Regex.Replace
它最终会这在内部:换句话说,如果模式不匹配,它将提前退出并返回原始输入。您的
Regex.IsMatch
用法正在做双重工作,但这是提前知道匹配项是否存在的唯一方法,因为Regex.Replace
返回一个字符串,但不指示是否存在匹配项比赛一开始就存在。将Match
与循环或Matches
一起使用不会让您轻松重建原始字符串,因为您需要找出匹配索引(使用Matches
Match
上的 >Index 属性)并将其拼凑在一起。如果您希望大量使用正则表达式,您可以考虑使用 RegexOptions.Compiled 选项。用它声明您的
Regex
对象并在整个代码中重用它。您需要对此进行一些研究,并衡量它是否值得(即,对于不需要它的场景来说,在任何地方使用该选项都太过分了)。If you inspect
Regex.Replace
with Reflector it eventually does this internally:In other words, if the pattern doesn't match it will exit early and return the original input. Your
Regex.IsMatch
usage is doing double work, but it's the only way to know in advance whether a match exists sinceRegex.Replace
returns a string with no indication of whether a match existed to begin with. UsingMatch
with a loop orMatches
won't make it easy for you to reconstruct the original string since you'll need to figure out the match indices (use theIndex
property onMatch
) and piece it all together.If you expect to use the regex alot you could look into using the
RegexOptions.Compiled
option. Declare yourRegex
object with it and reuse it throughout your code. You'll need to do some research on that and benchmark whether it's worth it (i.e., using that option everywhere is overkill for scenarios that don't need it).简短的答案是,创建一个 Regex 对象,然后当您进行搜索时,您将拥有一个可以调用替换的匹配集合。像这样:
这样,搜索只完成一次,然后只有在匹配时才可以执行操作。
Short answer is, create a Regex object, then when you do your search, you'll have a matches collection that you can call replace on. Something like this:
That way, the search is only done once, then you can action only if there is a match.