Skip to content

Regex ()(?'-1')(?!(?'-1')) exhibit incorrect matching behavior in .NET10 #120849

@longxya

Description

@longxya

Description

Such situation occur in (?!) and (?<!) under Interpreter、Compiled and source GeneratedCode.

Reproduction Steps

using System.Text.RegularExpressions;

var pattern = @"()(?'-1')(?!(?'-1'))";
var input = "such situation occur in (?!) and (?<!) under Interpreter、Compiled and source GeneratedCode";
Regex regex = new Regex(pattern, RegexOptions.Compiled);
var mhes = regex.Matches(input);
Console.WriteLine(Regex.Matches(input, pattern).Count);
Console.WriteLine(mhes.Count);
Console.WriteLine(RegexTester.regexGenerate().Matches(input).Count);
internal partial class RegexTester
{
	[GeneratedRegex(@"()(?'-1')(?!(?'-1'))")]
	internal static partial Regex regexGenerate();
}

Here is part of source Generated Code:

partial class RegexTester
{
    /// <remarks>
    /// Pattern:<br/>
    /// <code>()(?'-1')(?!(?'-1'))</code><br/>
    /// Explanation:<br/>
    /// <code>
    /// ○ Fail to match.<br/>
    /// </code>
    /// </remarks>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Text.RegularExpressions.Generator", "10.0.13.307")]
    internal static partial global::System.Text.RegularExpressions.Regex regexGenerate() => global::System.Text.RegularExpressions.Generated.regexGenerate_0.Instance;
}

private sealed class Runner : RegexRunner
{
    /// <summary>Scan the <paramref name="inputSpan"/> starting from base.runtextstart for the next match.</summary>
    /// <param name="inputSpan">The text being scanned by the regular expression.</param>
    protected override void Scan(ReadOnlySpan<char> inputSpan)
    {
        // The pattern never matches anything.
    }
}

Expected behavior

91
91
91

Actual behavior

0
0
0

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions