What C#/.NET 2.0 RegEx.Replace does if nothing needs to be replaced?
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 instanc开发者_如何转开发e only if needed. Maybe using RegEx.Matches or something.
Thanks & BR - Matti
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).
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.
精彩评论