Skip to content

Regex counted repetition of a greedy loop returns a too-short match #135584

Open

Description

@robertmorelli

Description

A counted repetition of a greedy inner loop returns a shorter match than the same loops written out separately. This repros in interpreter, Compiled, and NonBacktracking.

Reproduction Steps

using System;
using System.Text.RegularExpressions;

Console.WriteLine(Regex.Match("aaba", @"(?:(?:ab??)+){2}").Value); // aa
Console.WriteLine(Regex.Match("aaba", @"(?:ab??)+(?:ab??)+").Value); // aaba

Console.WriteLine(Regex.Match("aaaaK", @"(?:(?:a.?)+){2}").Value); // aaaa
Console.WriteLine(Regex.Match("aaaaK", @"(?:(?:a.?)+)(?:(?:a.?)+)").Value); // aaaaK

Expected behavior

Counted groups and manually repeated patterns should be equivalent.

Actual behavior

Counted groups match a shorter range.

Regression?

idk

Known Workarounds

always write out counted groups to get the actual greedy match?

Configuration

.NET 11.0.0-rc.1.26425.128
macOS ARM64

Other information

No response

Activity

  1. added
    untriaged
    on Oct 11, 2026
  2. dotnet-policy-service commented on Oct 11, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
    See info in area-owners.md if you want to be subscribed.

  3. added this to the Future milestone on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.RegularExpressionsuntriaged

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions