What would be the right answer if the requirement is that you need to ensure that the expression syntax is evaluated only once when the Regex object is initially instantiated.
var regExpPattern = @"somepatternhere";
Regex.Match(input, regExpPattern);var evaluate = new Regex(regExpPattern);var evaluate = new Regex(regExpPattern, RegexOptions.Compiled); what would be the answer 2 or 3?
The first one create a new Regex and calls matches() on it:
static MatchCollection Matches(String input, String pattern, RegexOptions options, TimeSpan matchTimeout) {
return new Regex(pattern, options, matchTimeout, true).Matches(input);
}
So first and second are almost the same things.
As it have been already said, the last one will be longueur to create but faster to use.
In short, if you plan to use the Regex just once, use 1) or 2). If you plan to run a loop or use the regex a lot of times, prefer the third solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With