Skip to content

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

@longxya

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

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions