diff --git a/ClosedXML/Excel/CalcEngine/CalcEngineHelpers.cs b/ClosedXML/Excel/CalcEngine/CalcEngineHelpers.cs index 4c1f2d8..6dea5a7 100644 --- a/ClosedXML/Excel/CalcEngine/CalcEngineHelpers.cs +++ b/ClosedXML/Excel/CalcEngine/CalcEngineHelpers.cs @@ -8,6 +8,21 @@ { internal class CalcEngineHelpers { + private static Lazy>> patternReplacements = + new Lazy>>(() => + { + var patternReplacements = new Dictionary>(); + // key: the literal string to match + // value: a tuple: first item: the search pattern, second item: the replacement + patternReplacements.Add(@"~~", new Tuple(@"~~", "~")); + patternReplacements.Add(@"~*", new Tuple(@"~\*", @"\*")); + patternReplacements.Add(@"~?", new Tuple(@"~\?", @"\?")); + patternReplacements.Add(@"?", new Tuple(@"\?", ".?")); + patternReplacements.Add(@"*", new Tuple(@"\*", ".*")); + + return patternReplacements; + }); + internal static bool ValueSatisfiesCriteria(object value, object criteria, CalcEngine ce) { // safety... @@ -60,23 +75,14 @@ // if criteria is a regular expression, use regex if (cs.IndexOfAny(new[] { '*', '?' }) > -1) - { - var patternReplacements = new Dictionary>(); - // key: the literal string to match - // value: a tuple: first item: the search pattern, second item: the replacement - patternReplacements.Add(@"~~", new Tuple(@"~~", "~")); - patternReplacements.Add(@"~*", new Tuple(@"~\*", @"\*")); - patternReplacements.Add(@"~?", new Tuple(@"~\?", @"\?")); - patternReplacements.Add(@"?", new Tuple(@"\?", ".?")); - patternReplacements.Add(@"*", new Tuple(@"\*", ".*")); - + { var pattern = Regex.Replace( cs, "(" + String.Join( "|", - patternReplacements.Values.Select(t => t.Item1)) + patternReplacements.Value.Values.Select(t => t.Item1)) + ")", - m => patternReplacements[m.Value].Item2); + m => patternReplacements.Value[m.Value].Item2); pattern = $"^{pattern}$"; return Regex.IsMatch(value.ToString(), pattern, RegexOptions.IgnoreCase);