Skip to content

Incorrect regex match range #135331

Closed
Closed

Description

@robertmorelli

Description

the regex pattern "(?:(?:a|)+?b){2}" matches outside the input string "ab" which is a bug. Additionally, the .Value accessor does not check that the match is a valid range.

Reproduction Steps

using System;
using System.Text.RegularExpressions;

var m = new Regex("(?:(?:a|)+?b){2}").Match("ab");
Console.WriteLine(m.Value);

https://dotnetfiddle.net/FqQSgN

Expected behavior

this should print an empty string

Actual behavior

The match range is out of bound of the input string resulting in the following error:
Unhandled exception. System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. (Parameter 'length')

Regression?

IDK

Known Workarounds

None

Configuration

.NET 10.0.12
macOS 27.0
ARM64

Other information

No response

Activity

  1. added
    untriaged
    on Oct 7, 2026
  2. dotnet-policy-service commented on Oct 7, 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. MihaZupan commented on Oct 7, 2026

    @MihaZupan
    Member

    Similar as #135328, this may have been fixed by #120872

  4. MihaZupan commented on Oct 19, 2026

    @MihaZupan
    Member

    I'll close this as fixed, feel free to comment again if you see otherwise

  5. removed
    untriaged
    on Oct 19, 2026
  6. locked and limited conversation to collaborators on Nov 19, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions