Skip to content

Compiled conditional regex match different with Interpretor, and it throws exception #126556

Closed

Description

@longxya

Description

in compiled
Use (?((?'-1'))|((?'1'\S)+|(?'1'\s)))+(?!(?'-1')) to match abc, results are "","","".
It's different with interpretor and source generater.

This situation occured in .NET7、.NET8、.NET9、.NET10, work normally in .NET5 and .NET6.

Reproduction Steps

using System;
using System.Text.RegularExpressions;

{
	var pattern = @"(?((?'-1'))|(?'outer'(?'1'\S)+|(?'1'\s)))+(?!(?'-1'))";
	pattern = @"(?((?'-1'))|(?:(?'1'\S)+|(?'1'\s)))+(?!(?'-1'))";
	pattern = @"(?((?'-1'))|((?'1'\S)+|(?'1'\s)))+(?!(?'-1'))";
	
	var input = "abc";
	var regexCompiled = new Regex(pattern, RegexOptions.Compiled);
	var regexInterpreted = new Regex(pattern, RegexOptions.None);
	
	Console.WriteLine($"----------Compiled results count:{regexCompiled.Matches(input).Count}------------");
	foreach(Match mh in regexCompiled.Matches(input))
	{
		Console.WriteLine($"Compiled: [{mh.Value}]");
	}
	Console.WriteLine($"----------Interpreted results count:{regexInterpreted.Matches(input).Count}------------");
	foreach(Match mh in regexInterpreted.Matches(input))
	{
		Console.WriteLine($"Interpreted: [{mh.Value}]");
	}
}
Console.WriteLine("#################################################################");
{
	var pattern = @"(?((?'-1'){6})|((?'1'(?'2'\S))+|(?'1'(?'2'\s))))+(?!(?'-1'))";
	var input = "it not";
	var regexCompiled = new Regex(pattern, RegexOptions.Compiled);
	var len = regexCompiled.Matches(input).Count;
}

Expected behavior

----------Compiled results count:0------------
----------Interpreted results count:0------------
#################################################################

Actual behavior

----------Compiled results count:3------------
Compiled: []
Compiled: []
Compiled: []
----------Interpreted results count:0------------
#################################################################
Unhandled exception. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
   at Regex2_TryMatchAtCurrentPosition(RegexRunner, ReadOnlySpan`1)
   at Regex2_Scan(RegexRunner, ReadOnlySpan`1)
   at System.Text.RegularExpressions.Regex.ScanInternal(RegexRunnerMode mode, Boolean reuseMatchObject, String input, Int32 beginning, RegexRunner runner, ReadOnlySpan`1 span, Boolean returnNullIfReuseMatchObject)
   at System.Text.RegularExpressions.Regex.RunSingleMatch(RegexRunnerMode mode, Int32 prevlen, String input, Int32 beginning, Int32 length, Int32 startat)
   at System.Text.RegularExpressions.MatchCollection.GetMatch(Int32 i)
   at System.Text.RegularExpressions.MatchCollection.get_Count()
   at Program.<Main>$(String[] args)

Regression?

No. Occur in .NET8, too

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. dotnet-policy-service commented on Apr 5, 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.

  2. added
    untriaged
    on Apr 5, 2026
  3. longxya commented on Apr 5, 2026

    @longxya
    Author

    In .NET10, compiled regex will timeout.
    But in .NET9, compiled regex incorrectly match 3 empty value.

    using System;
    using System.Text.RegularExpressions;
    
    var pattern = @"(?((?'-1'))|((?'1'\S)|))+(?!(?'-1'))";
    //pattern = @"(?((?'-1'))|((?'1'\S)|(?'1'\s)))+(?!(?'-1'))";
    var input = "abc";
    
    var timeout = TimeSpan.FromMilliseconds(10);
    var regexCompiled = new Regex(pattern, RegexOptions.Compiled, timeout);
    var regexInterpreted = new Regex(pattern, RegexOptions.None, timeout);
    
    try
    {
    	Console.WriteLine($"----------Interpreted results count:{regexInterpreted.Matches(input).Count}------------");
    	foreach(Match mh in regexInterpreted.Matches(input))
    	{
    		Console.WriteLine($"Interpreted: [{mh.Value}]");
    	}
    	Console.WriteLine($"----------Compiled results count:{regexCompiled.Matches(input).Count}------------");
    	foreach(Match mh in regexCompiled.Matches(input))
    	{
    		Console.WriteLine($"Compiled: [{mh.Value}]");
    	}
    }
    catch(Exception ex)
    {
    	Console.WriteLine(ex.Message);
    }

    Output in .NET10:

    ----------Interpreted results count:0------------
    The Regex engine has timed out while trying to match a pattern to an input string. This can occur for many reasons, including very large inputs or excessive backtracking caused by nested quantifiers, back-references and other factors.
    

    Output in .NET[7-9]:

    ----------Interpreted results count:0------------
    ----------Compiled results count:3------------
    Compiled: []
    Compiled: []
    Compiled: []
    
  4. self-assigned this
    on Apr 5, 2026
  5. removed
    untriaged
    on Apr 8, 2026
  6. locked and limited conversation to collaborators on May 9, 2026
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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions