diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs index ea3ced24ee08e..43c2fdf416910 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs @@ -2553,18 +2553,8 @@ void EmitCapture(RegexNode node, RegexNode? subsequent = null) } writer.WriteLine(); - RegexNode child = node.Child(0); - - if (uncapnum != -1) - { - using (EmitBlock(writer, $"if (!base.IsMatched({uncapnum}))")) - { - Goto(doneLabel); - } - writer.WriteLine(); - } - // Emit child node. + RegexNode child = node.Child(0); string originalDoneLabel = doneLabel; EmitNode(child, subsequent); bool childBacktracks = doneLabel != originalDoneLabel; @@ -2577,6 +2567,12 @@ void EmitCapture(RegexNode node, RegexNode? subsequent = null) } else { + using (EmitBlock(writer, $"if (!base.IsMatched({uncapnum}))")) + { + Goto(doneLabel); + } + + writer.WriteLine(); writer.WriteLine($"base.TransferCapture({capnum}, {uncapnum}, {startingPos}, pos);"); } diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs index 7f168fc65dbd9..c1948b957f939 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs +++ b/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs @@ -2400,18 +2400,8 @@ void EmitCapture(RegexNode node, RegexNode? subsequent = null) Ldloc(pos); Stloc(startingPos); - RegexNode child = node.Child(0); - - if (uncapnum != -1) - { - // if (!IsMatched(uncapnum)) goto doneLabel; - Ldthis(); - Ldc(uncapnum); - Call(IsMatchedMethod); - BrfalseFar(doneLabel); - } - // Emit child node. + RegexNode child = node.Child(0); Label originalDoneLabel = doneLabel; EmitNode(child, subsequent); bool childBacktracks = doneLabel != originalDoneLabel; @@ -2431,6 +2421,12 @@ void EmitCapture(RegexNode node, RegexNode? subsequent = null) } else { + // if (!IsMatched(uncapnum)) goto doneLabel; + Ldthis(); + Ldc(uncapnum); + Call(IsMatchedMethod); + BrfalseFar(doneLabel); + // TransferCapture(capnum, uncapnum, startingPos, pos); Ldthis(); Ldc(capnum); 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 1bbc7cf607c68..df8858c981383 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 @@ -564,6 +564,8 @@ public static IEnumerable Match_MemberData() { // Throws NotSupported with NonBacktracking engine because of the balancing group dog-0 yield return (@"(?cat)\w+(?dog)", "cat_Hello_World_dog", RegexOptions.None, 0, 19, false, string.Empty); + yield return (@"(.)(?'2-1'(?'-1'))", "cat", RegexOptions.None, 0, 3, false, string.Empty); + yield return (@"(?'2-1'(.))", "cat", RegexOptions.None, 0, 3, true, "c"); } // Atomic Zero-Width Assertions \A \Z \z \b \B