I'm sometimes having RegexMatchTimeoutException
when parsing a short (less than 100 characters) string. The parse itself is inside a function in a list.Select(..)
of a collection of about 30 elements.
I suspect it may be due to sub-optimal Regex - here's the definition in C#:
internal override Regex Regex => new(
@$"((.|\s)*\S(.|\s)*)(\[{this.Type}\])", // Type = "input"
RegexOptions.Multiline | RegexOptions.Compiled,
TimeSpan.FromMilliseconds(Constants.RegexTimeout)); // RegexTimeout = 100
It should capture Sample text
in the following string:
Sample text
[input]
Full exception message:
System.Text.RegularExpressions.RegexMatchTimeoutException: 'The Regex engine has timed out while trying to match a pattern to an input string. This can occur for many reasons, including very large inputs or excessive backtracking caused by nested quantifiers, back-references and other factors.'
Line in which the exception occurs:
var label = this.Regex.Match(sectionContent).Groups[1].Value.Trim();
The exception is rather hard to reproduce - with the same input it can happen on the first run or on the 100th. But the bigger the collection of lines to run the Regex against, the bigger the chance of it occurring.