Skip to content

Regex lazy quantifier with optional group behaves differently in .NET 10 vs .NET 9 #125321

@vb-kalei

Description

@vb-kalei

Description

A regex pattern that matches correctly in .NET 9 fails in .NET 10 when used with Regex.Match. The behavior appears to have changed in the regex engine's backtracking when a lazy quantifier is inside an optional group.

Reproduction Steps

using System;
using System.Text.RegularExpressions;

var text = @"abccd";

var regex = new Regex(@"a(b.*?c)?d");

var match = regex.Match(text);

Console.WriteLine(match.Success);

Run this code (eg. as a console application or in https://dotnetfiddle.net/) in .NET 9 vs .NET 10.

This seems to be a minimal repro, if I change anything, it works.

Expected behavior

I would expect the regex to match in .NET 10. (I found no such breaking change in the official list.)

Actual behavior

In .NET 10 the regex does not match (code outputs "False").

Regression?

Yes, seems to be a regression as it worked in .NET 9.

Known Workarounds

Compiling with .NET 9.

Configuration

.NET SDK:
Version: 10.0.200-preview.0.26103.119
Commit: a2bfa4671c
Workload version: 10.0.200-manifests.796b3495
MSBuild version: 18.3.0-release-26103-119+a2bfa4671

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26200
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\10.0.200-preview.0.26103.119\

Other information

No response

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions