From 362a46aa4584839f5d2e8f2ecc8cc28aa6f79ea1 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 11 Mar 2026 22:52:52 -0400 Subject: [PATCH] Add tests for lazy quantifier inside optional group Add test cases for the pattern a(b.*?c)?d to confirm the fix from PR #124254 addresses the regression reported in #125321, where a lazy quantifier inside an optional group incorrectly failed to match. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../tests/FunctionalTests/Regex.Match.Tests.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Match.Tests.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Match.Tests.cs index c168ed04c54396..af65e57e203a4d 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Match.Tests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.Match.Tests.cs @@ -385,6 +385,11 @@ public static IEnumerable Match_MemberData() yield return (@"(\(.*?\))+?;", "(a)b);", RegexOptions.None, 0, 6, true, "(a)b);"); yield return (@"(\(.*?\))*;", "(a)b);", RegexOptions.None, 0, 6, true, "(a)b);"); yield return (@"(\(.*?\))*?;", "(a)b);", RegexOptions.None, 0, 6, true, "(a)b);"); + // Lazy quantifier inside optional group where the lazy loop must expand for the rest of the pattern to match. + yield return (@"a(b.*?c)?d", "abccd", RegexOptions.None, 0, 5, true, "abccd"); + yield return (@"a(b.*?c)?d", "abcd", RegexOptions.None, 0, 4, true, "abcd"); + yield return (@"a(b.*?c)?d", "ad", RegexOptions.None, 0, 2, true, "ad"); + yield return (@"a(b.*?c)?d", "abce", RegexOptions.None, 0, 4, false, string.Empty); // Non-backtracking body: optimization correctly applies. These verify bodies without backtracking // inside the outer loop are still correctly matched after the outer loop is made atomic. yield return (@"\w+(\(abc\))?,", "Foo(abc),", RegexOptions.None, 0, 9, true, "Foo(abc),");