You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Regex throw an exception Specified argument was out of the range of valid values. in compiled mode #119444
I'm trying to simplify the pattern.
And there are also some issues with this pattern in source generation.
Reproduction Steps
usingSystem;usingSystem.Text.RegularExpressions;//The pattern when this bug was first discoveredstringpattern=@"^(?=\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 generatedpattern=@"(?'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}";stringinput="25525511135";Regexregex=newRegex(pattern,RegexOptions.Compiled);varmhes=regex.Matches(input);try{Console.WriteLine(mhes.Count);}catch(Exceptione){Console.WriteLine(e.Message);}try{Console.WriteLine("Group#2's captures count : "+Regex.Match(input,pattern).Groups[2].Captures.Count);}catch(Exceptione){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
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.
usingSystem;usingSystem.Text.RegularExpressions;varpattern=@"(?=(?'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, weirdvarinput="222222";RegexregexCompiled=newRegex(pattern,RegexOptions.Compiled);varCompiled=regexCompiled.Matches(input);varinterpreted=Regex.Matches(input,pattern);for(vari=0;i<Compiled.Count;i++){Console.WriteLine($"Compiled == interpreted : "+(Compiled[i].Groups[1].Captures.Count==interpreted[i].Groups[1].Captures.Count));}Console.WriteLine("--------------------------------");try{varr=regexCompiled.Match("2").Value;}catch(Exceptionex){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);varr=newRegex(pattern,RegexOptions.Compiled).Match(input).Value;}catch(Exceptionex){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.
Activity
dotnet-policy-service commentedon Sep 7, 2025
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.
longxya commentedon Sep 16, 2025
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.Output:
stephentoub commentedon Oct 19, 2025
This is a duplicate of the issue #111631 you previously filed and that was fixed by #111769. It does not repro on .NET 10.