dotnet / dotnet/runtime

Regex (?:(?'G')|){n}(?#n > 0) always capture only once in .NET10

Open
#132,964 5 comments 1 reaction 2 assignees Claimed by @jeffhandley View on GitHub
area-System.Text.RegularExpressions bug regression-from-last-release
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### Description

In .NET10, regex `(?:(?'G')|){n}(?#n > 0)` always capture only once.

### Reproduction Steps

```c#
using System;
using System.Text.RegularExpressions;

var input = "1";
var pattern = @"\w(?:(?'G')|){3}";
{
Console.WriteLine("--------------------interpreter---------------------");
var mh = Regex.Match(input, pattern);
foreach(Capture capture in mh.Groups["G"].Captures)
{
Console.WriteLine($"Index:{capture.Index}, Length:{capture.Length}");
}
}
{
Console.WriteLine("--------------------Compiled---------------------");
var mh = new Regex(pattern, RegexOptions.Compiled).Match(input);
foreach(Capture capture in mh.Groups["G"].Captures)
{
Console.WriteLine($"Index:{capture.Index}, Length:{capture.Length}");
}
}
```

### Expected behavior

```
--------------------interpreter---------------------
Index:1, Length:0
Index:1, Length:0
Index:1, Length:0
--------------------Compiled---------------------
Index:1, Length:0
Index:1, Length:0
Index:1, Length:0
```

### Actual behavior

```
--------------------interpreter---------------------
Index:1, Length:0
--------------------Compiled---------------------
Index:1, Length:0
```

### Regression?

_No response_

### Known Workarounds

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.