如果不需要替换任何内容,C#/.NET 2.0 RegEx.Replace 会做什么?

发布于 2024-09-30 19:46:32 字数 700 浏览 4 评论 0原文

当调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨后彩虹 2024-10-07 19:46:32

如果您使用 Reflector 检查 Regex.Replace 它最终会这在内部:

Match match = regex.Match(input, startat);
if (!match.Success)
{
    return input;
}

换句话说,如果模式不匹配,它将提前退出并返回原始输入。您的 Regex.IsMatch 用法正在做双重工作,但这是提前知道匹配项是否存在的唯一方法,因为 Regex.Replace 返回一个字符串,但不指示是否存在匹配项比赛一开始就存在。将 Match 与循环或 Matches 一起使用不会让您轻松重建原始字符串,因为您需要找出匹配索引(使用 Matches Match 上的 >Index 属性)并将其拼凑在一起。

如果您希望大量使用正则表达式,您可以考虑使用 RegexOptions.Compiled 选项。用它声明您的 Regex 对象并在整个代码中重用它。您需要对此进行一些研究,并衡量它是否值得(即,对于不需要它的场景来说,在任何地方使用该选项都太过分了)。

If you inspect Regex.Replace with Reflector it eventually does this internally:

Match match = regex.Match(input, startat);
if (!match.Success)
{
    return input;
}

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 since Regex.Replace returns a string with no indication of whether a match existed to begin with. Using Match with a loop or Matches won't make it easy for you to reconstruct the original string since you'll need to figure out the match indices (use the Index property on Match) and piece it all together.

If you expect to use the regex alot you could look into using the RegexOptions.Compiled option. Declare your Regex 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).

痞味浪人 2024-10-07 19:46:32

简短的答案是,创建一个 Regex 对象,然后当您进行搜索时,您将拥有一个可以调用替换的匹配集合。像这样:

var regex = new Regex(pattern, RegexOptions.Compiled);
foreach (var currentMatch in regex.Matches)
{
    //Do stuff
}

这样,搜索只完成一次,然后只有在匹配时才可以执行操作。

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:

var regex = new Regex(pattern, RegexOptions.Compiled);
foreach (var currentMatch in regex.Matches)
{
    //Do stuff
}

That way, the search is only done once, then you can action only if there is a match.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文