Skip to content

Regex Capture gives IndexOutOfRangeException on (?:){93} #62094

@danmoseley

Description

@danmoseley
Regex regex = new("@(a*)+?");
MatchCollection mc = regex.Matches("@");

foreach (Match m in mc)
{
    Console.WriteLine($"{m.Index} .. {m.Length + m.Index}");
    Console.WriteLine(m.ToString());
}

On other regex engines, like PCRE, Nim, Python, Javascript, and Golang you would get 0 .. 1

On .NET Framework this displays 1 .. 1

On .NET 6 it gives

1 .. 3
Unhandled exception. System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. (Parameter 'length')
   at System.String.Substring(Int32 startIndex, Int32 length)
   at System.Text.RegularExpressions.Capture.get_Value()
   at System.Text.RegularExpressions.Capture.ToString()
   at Program.<Main>$(String[] args) in C:\Users\danmose\source\repos\ConsoleApp31\ConsoleApp31\Program.cs:line 191

@stephentoub first bug from running Nim tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.RegularExpressionsbugdisabled-testThe test is disabled in source code against the issuehelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is merged

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions