非常简单的正则表达式匹配需要永远计算

时间:2016-03-23 13:54:56

标签: c# regex performance

以下代码需要永远执行。

var input = "105,106,109,110,111,112,113,114,115,116,117,118,1423,1424s,1426,1427,1429,1431,34,39,44s";
var pattern = new Regex(@"^(-?[0-9]+,?)+$");

var stopWatch = Stopwatch.StartNew();
var result = pattern.IsMatch(input);
stopWatch.Stop();

Console.WriteLine("Finished matching in {0}ms. Result: {1}", stopWatch.ElapsedMilliseconds, result);

如果数字较少(或末尾有s的数字),则匹配更快完成。

那里发生了什么?

0 个答案:

没有答案