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
Result of Regex.Match("wtfb",@"(.)()+?b") is not same as new Regex(@"(.)()+?b",RegexOptions.Compiled).Match("wtfb") #111051
using System.Text.RegularExpressions;
string input = "wtfb";
string pattern="^(.)+()+?b";
Match matchInterpreted = new Regex(pattern, RegexOptions.None).Match(input);
Match matchCompiled = new Regex(pattern, RegexOptions.Compiled).Match(input);
Console.WriteLine($"Interpreted: {matchInterpreted.Value}");
Console.WriteLine($"Compiled: {matchCompiled.Value}");
Output:
Interpreted: b
Compiled: wtfb
Expected behavior
Output:
Interpreted: wtfb
Compiled: wtfb
Actual behavior
Output:
Interpreted: b
Compiled: wtfb
It looks like ()+? would make nearest outside brackets start at where the empty loop began, and this effect maybe gradually spread to group 0 or not.
Verified - the interpreted mode has a different result than both compiled and generated via [GeneratedRegex] and .NET v8, v9 and current v10 have the same issue.
I'll mark this as a bug and for v10 however with "help wanted".
Similar regular expressions can cause infinite matching
I'm adding this test case to the current issue for completeness and caution, as I'm uncertain whether it shares the exact same root cause with the cases submitted in March.
Although both involve the ()+? structure, this new case demonstrates infinite matching under meaningfully different conditions—specifically, within a lookbehind assertion. This suggests the underlying engine issue might manifest in a distinct code path.
Another type of regular expression can trigger infinite matching
usingSystem;usingSystem.Text.RegularExpressions;stringinput="What is the fuck! WTF";stringpattern="WTF(?<=(.()+?)WTF)";varmhes=newRegex(pattern,RegexOptions.None).Matches(input);try{//var matchCount = mhes.Count;// will throw TimeOutException if memory is enoughfor(vari=0;i<1000;i++){Console.WriteLine(mhes[i].Value);}}catch(Exceptione){Console.WriteLine(e.Message);}
output:
t is the fuck!
t is the fuck!
t is the fuck!
.
.
.
@stephentoub@ericstj Potential ReDoS due to infinite matching in regular expressions containing ()+?
Regular expressions containing the ()+? construct may cause infinite matching, potentially leading to ReDoS attacks. However, in practice, most real-world regular expressions don't typically contain such ()+? subpatterns.
Despite this, due to the engine bug with ()+?, regular expressions containing variants of this structure may still trigger ReDoS vulnerabilities.
Activity
dotnet-policy-service commentedon Jan 3, 2025
Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.
DL444 commentedon Jan 3, 2025
Typo in the expected behavior. It is expected that the interpreted result matches compiled.
steveharter commentedon Jan 14, 2025
Verified - the interpreted mode has a different result than both compiled and generated via
[GeneratedRegex]and .NET v8, v9 and current v10 have the same issue.I'll mark this as a bug and for v10 however with "help wanted".
cc @stephentoub
longxya commentedon Jan 14, 2025
Do I need to submit new issues for other regular expression problems that appear to be bugs? Such as:
(?'2-1'(?'1'.)), the interpreted mode has a different result than both compiled and generated via [GeneratedRegex].ovidiucosteanet commentedon Jan 16, 2025
Hello! I'd like to create a PR for this. Can you please assign the issue to me?
stephentoub commentedon Feb 4, 2025
@ovidiucosteanet, if you'd like to give it a go, we'd welcome the help. Thanks.
longxya commentedon Mar 25, 2025
@stephentoub @ovidiucosteanet
Similar regular expressions can cause infinite matching
Maybe it is related to this issue
Output:
And I do not know why changing the quantifier to something like
{2,100}would directly throw an exceptionSystem.OutOfMemoryExceptionOutput:
2 remaining items
(((((((((((((((((?'wtf'(a+|())+)+?)+)+)+)+)+)+)+)+)+)+)+)+)+)+)+)+#119073 as a duplicate of this issuelongxya commentedon Oct 16, 2025
I'm adding this test case to the current issue for completeness and caution, as I'm uncertain whether it shares the exact same root cause with the cases submitted in March.
Although both involve the
()+?structure, this new case demonstrates infinite matching under meaningfully different conditions—specifically, within a lookbehind assertion. This suggests the underlying engine issue might manifest in a distinct code path.Another type of regular expression can trigger infinite matching
output:
longxya commentedon Oct 17, 2025
@stephentoub @ericstj
Potential ReDoS due to infinite matching in regular expressions containing
()+?Regular expressions containing the
()+?construct may cause infinite matching, potentially leading to ReDoS attacks. However, in practice, most real-world regular expressions don't typically contain such()+?subpatterns.Despite this, due to the engine bug with
()+?, regular expressions containing variants of this structure may still trigger ReDoS vulnerabilities.Example1:
output:
Example2:
output:
Fix longstanding regex interpreter bug around lazy loops with empty m…
(\d*?)*?\d$(?'-1'){2}#123184 as a duplicate of this issue