以下代码需要永远执行。
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
的数字),则匹配更快完成。
那里发生了什么?