-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Regex.Match(string, int, int) throws an exception when using a pattern with '^' or '$' and the Multiline option #55557
Comments
Tagging subscribers to this area: @eerhardt, @dotnet/area-system-text-regularexpressions Issue DetailsWhen using the Regex.Match function that also takes the start index and the length on the input string, and matching the input against a pattern containing '^' and '$' (using the multiline option), the function wrongfully throws an argument out of range exception if a match can be 'started' but cannot be found. string input = "ABC\n";
Regex pattern = new Regex(@"^A$", RegexOptions.Multiline);
Match m = pattern.Match(input, 0, 2); // <-- Argument out of range exception (Parameter 'start'), for lengths 2 or 3. (The match can be 'started' because of the 'A', but then the '$' cannot be matched to either end-of-line or end-of-text, I guess). Other combinations, like the pattern '^A' (without '$'), also caused the exception (For different yet similar input strings, start indices and lengths) but we couldn't quite figure out when and why. Configuration:OS: Windows 10 Enterprise, 20H2 Regression:This worked fine using .NET Framework 4.7. Other information:The exact exception says:
|
Thanks. This appears to be a regression introduced as part of all the work we did on regex in .NET 5:
|
When using the Regex.Match function that also takes the start index and the length on the input string, and matching the input against a pattern containing '^' and '$' (using the multiline option), the function wrongfully throws an argument out of range exception if a match can be 'started' but cannot be found.
(The match can be 'started' because of the 'A', but then the '$' cannot be matched to either end-of-line or end-of-text, I guess).
Other combinations, like the pattern '^A' (without '$'), also caused the exception (For different yet similar input strings, start indices and lengths) but we couldn't quite figure out when and why.
Configuration:
OS: Windows 10 Enterprise, 20H2
Arch: x64
Target framework: .NET5
Installed:
.NET SDK 5.0.301 (x64)
Microsoft.AspNetCore.App 5.0.7
Microsoft.NETCore.App 5.0.7
Microsoft.WindowsDesktop.App 5.0.7
(And some previous versions too.)
Visual studio:
Professional 2019 16.10.2
Regression:
This worked fine using .NET Framework 4.7.
Other information:
The exact exception says:
System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. (Parameter 'start')'
With the inner exception being:
at System.Text.RegularExpressions.RegexInterpreter.FindFirstChar()
at System.Text.RegularExpressions.RegexRunner.Scan(Regex regex, String text, Int32 textbeg, Int32 textend, Int32 textstart, Int32 prevlen, Boolean quick, TimeSpan timeout)
at System.Text.RegularExpressions.Regex.Run(Boolean quick, Int32 prevlen, String input, Int32 beginning, Int32 length, Int32 startat)
at System.Text.RegularExpressions.Regex.Match(String input, Int32 beginning, Int32 length)
at the code above
The text was updated successfully, but these errors were encountered: