Skip to content

Regex throw an exception Specified argument was out of the range of valid values. in compiled mode #119444

Closed as duplicate

Description

@longxya

Description

I'm trying to simplify the pattern.
And there are also some issues with this pattern in source generation.

Reproduction Steps

using System;
using System.Text.RegularExpressions;

//The pattern when this bug was first discovered
string pattern = @"^(?=\d{3,12})(?'time'){12}(((?=((?'-time')(?'y')|(?'x')){12}((?'time-y')|){12}((^[1-9](?'count')(((?'-count')|((?'-x')|(?'-x1'))\d|(?'x1'\d))(?'count')){2}|0)(?=\d{3,9}$)(?((?'-count'){3})(?<!^\d([6-9]\d|5[6-9]))|((?'-count')|){3})([1-9](?'count')(((?'-count')|((?'-x')|(?'-x2'))\d|(?'x2'\d))(?'count')){2}|0)(?=\d{2,6}$)(?((?'-count'){3})(?<![3-9]\d\d|\d([6-9]\d|5[6-9]))|((?'-count')|){3})([1-9](?'count')(((?'-count')|((?'-x')|(?'-x3'))\d|(?'x3'\d))(?'count')){2}|0)(?=\d{1,3}$)(?((?'-count'){3})(?<![3-9]\d\d|\d([6-9]\d|5[6-9]))|((?'-count')|){3})(?(\d\d\d$)(?![3-9]\d\d|\d([6-9]\d|5[6-9])))([1-9]\d*|0)$(?!(?'-x'))((?'-x')|){12}))|)(?'-time')){12}";
//this pattern would work normally in compiled when input = "101023"
pattern = @"(?'time'){2}(?=((?'-time')|(?'x')){3}((^[1-9](?'count')(((?'-count')|((?'-x'))\d)(?'count')){2}|0)(?((?'-count'){3})|((?'-count')|){3})([1-9](?'count')(((?'-count')|((?'-x'))\d)(?'count')){2}|0)(?!(?'-x')))){2}";
//this pattern is also  bad in compiled mode, but it does seem to cause Group#2 to permanently fail to match in source generated
pattern = @"(?'time'){2}(?=((?'-time')|(?'x')){3}(((?'count')(((?'-count')|((?'-x')))(?'count')){2}|0)(?((?'-count'){3})|((?'-count')|){3})(?!(?'-x')))){2}";
//This pattern seems to have issues in both compiled mode and source-generated code. 
pattern = @"(?'time'){2}(?=((?'-time')|(?'x')){3}(((?'count')(((?'-count')|((?'-x')))(?'count')){2}|0)(?((?'-count'){3})|((?'-count')|){3})(?!(?'-x')))|){2}";
string input = "25525511135";
Regex regex = new Regex(pattern, RegexOptions.Compiled);
var mhes = regex.Matches(input);
try
{
	Console.WriteLine(mhes.Count);
}catch(Exception e)
{
	Console.WriteLine(e.Message);
}
try
{
	Console.WriteLine("Group#2's captures count : "+Regex.Match(input, pattern).Groups[2].Captures.Count);
}catch(Exception e)
{
	Console.WriteLine(e.Message);
}

Expected behavior

12
Group#2's captures count : 1

Actual behavior

Specified argument was out of the range of valid values.
Group#2's captures count : 1

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Sep 7, 2025
  2. added
    needs-area-label
    on Sep 7, 2025
  3. added and removed
    needs-area-label
    on Sep 7, 2025
  4. dotnet-policy-service commented on Sep 7, 2025

    @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.

  5. longxya commented on Sep 16, 2025

    @longxya
    Author

    It looks like there is some wrong when compiler compiling this regex.
    For an assertion like (?=exp){1,}, it should be equal to (?=exp){1} or (?=exp). but in this case, it works not very correct in compiled mode.

    using System;
    using System.Text.RegularExpressions;
    					
    var pattern = @"(?=(?'x')((((|(?'count'))){2})(?((?'-count'))|((?!x)(?=x)|))(?!(?'-x')))|){1,}";
    //pattern = @"(?=(?'x')((((|(?'count'))){2})(?((?'-count'))|((?!x)(?=x)|))(?!(?'-x')))|){1}";//Change the quantifier from `{1,}` to `{1}` will work normally, weird
    //pattern = @"(?=(?'x')((((|(?'count'))){2})(?((?'-count'))|((?!)|))(?!(?'-x')))|){1,}";//change `(?!x)(?=x)` to `(?!)`, will work normally, weird
    var input = "222222";
    Regex regexCompiled = new Regex(pattern , RegexOptions.Compiled);
    var Compiled = regexCompiled.Matches(input);
    var interpreted = Regex.Matches(input, pattern);
    for(var i = 0; i < Compiled.Count; i++)
    {
    	Console.WriteLine($"Compiled == interpreted : " + (Compiled[i].Groups[1].Captures.Count == interpreted[i].Groups[1].Captures.Count));
    }
    Console.WriteLine("--------------------------------");
    try
    {
    	var r = regexCompiled.Match("2").Value;
    }
    catch(Exception ex)
    {
    	Console.WriteLine("Compiled error : " + ex.Message);
    }
    try
    {
    	pattern = @"(?'time'){2}(?=((?'-time')|(?'x')){3}(((?'count')(((?'-count')|)(?'count')){2})(?((?'-count'){3})|((?'why')|))(?!(?'-x')))|){2}";
    	input = "22";
    	Console.WriteLine("interpreted : " + Regex.Matches(input, pattern).Count);
    	var r = new Regex(pattern, RegexOptions.Compiled).Match(input).Value;
    }
    catch(Exception ex)
    {
    	Console.WriteLine("Compiled error : " + ex.Message);
    }

    Output:

    Compiled == interpreted : True
    Compiled == interpreted : True
    Compiled == interpreted : False
    Compiled == interpreted : False
    Compiled == interpreted : False
    Compiled == interpreted : False
    Compiled == interpreted : False
    --------------------------------
    Compiled error : Specified argument was out of the range of valid values.
    interpreted : 3
    Compiled error : Exception of type 'System.OutOfMemoryException' was thrown.
    
  6. added this to the Future milestone on Sep 24, 2025
  7. removed
    untriaged
    on Sep 24, 2025
  8. stephentoub commented on Oct 19, 2025

    @stephentoub
    Member

    This is a duplicate of the issue #111631 you previously filed and that was fixed by #111769. It does not repro on .NET 10.

  9. locked and limited conversation to collaborators on Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions