Skip to content

Remove capture groups from negative lookarounds #118084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,43 @@ private RegexNode ReduceLookaround()
Debug.Assert(Kind is RegexNodeKind.PositiveLookaround or RegexNodeKind.NegativeLookaround);
Debug.Assert(ChildCount() == 1);

// Captures inside of negative lookarounds are undone after the lookaround. Thus, if there's nothing
// inside of the negative lookaround that needs that capture group (namely a backreference), we can
// remove the capture.
if (Kind is RegexNodeKind.NegativeLookaround && ContainsBackreference(Child(0)) is false)
{
if (RemoveCaptures(this, 0))
{
// If we removed captures, we may have changed the structure of the tree in a way that exposed more
// optimization possibility, so re-reduce the children.
ReplaceChild(0, Child(0));
}

static bool RemoveCaptures(RegexNode parent, int nodeIndex)
{
RegexNode node = parent.Child(nodeIndex);

if (node.Kind is RegexNodeKind.Capture)
{
parent.ReplaceChild(nodeIndex, node.Child(0));
RemoveCaptures(parent, nodeIndex);
return true;
}

bool changesMade = false;
if (StackHelper.TryEnsureSufficientExecutionStack())
{
int childCount = node.ChildCount();
for (int i = 0; i < childCount; i++)
{
changesMade |= RemoveCaptures(node, i);
}
}

return changesMade;
}
}

// A lookaround is a zero-width atomic assertion.
// As it's atomic, nothing will backtrack into it, and we can
// eliminate any ending backtracking from it.
Expand All @@ -2043,6 +2080,32 @@ private RegexNode ReduceLookaround()
return this;
}

/// <summary>Gets whether the node contains a backreference anywhere in its tree.</summary>
private static bool? ContainsBackreference(RegexNode node)
{
if (node.Kind is RegexNodeKind.Backreference or RegexNodeKind.BackreferenceConditional)
{
return true;
}

if (!StackHelper.TryEnsureSufficientExecutionStack())
{
// If we can't recur further, just stop optimizing.
return null;
}

int childCount = node.ChildCount();
for (int i = 0; i < childCount; i++)
{
if (ContainsBackreference(node.Child(i)) is true)
{
return true;
}
}

return false;
}

/// <summary>Optimizations for backreference conditionals.</summary>
private RegexNode ReduceBackreferenceConditional()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ public class RegexReductionTests
[InlineData("(?>(?>(?>(?>))))", "")]
[InlineData("(?>(?>(?>(?>(?!)))))", "(?!)")]
[InlineData("(?=(?>))", "")]
// Lookaround reduction
[InlineData("(?!(abc))", "(?!abc)")]
[InlineData("(?!a(b*)c)", "(?!ab*c)")]
[InlineData("(?!a((((b))))c)", "(?!abc)")]
// Alternation reduction
[InlineData("a|b", "[ab]")]
[InlineData("a|b|c|d|e|g|h|z", "[a-eghz]")]
Expand Down Expand Up @@ -518,6 +522,10 @@ public void PatternsReduceIdentically(string actual, string expected)
[InlineData("(abc?)*?d", "(?>(ab(?>c?))*)d")]
[InlineData("(aba)+d", "(?>(aba)+)d")]
[InlineData("(abc*)*d", "(?>(ab(?>c*))*)d")]
// Lookaround reduction
[InlineData("(?=(abc))", "(?=abc)")]
[InlineData("(?=a(b*)c)", "(?=ab*c)")]
[InlineData("(?=a((((b))))c)", "(?=abc)")]
// Loops inside alternation constructs
[InlineData("(abc*|def)chi", "(ab(?>c*)|def)chi")]
[InlineData("(abc|def*)fhi", "(abc|de(?>f*))fhi")]
Expand Down
Loading