Skip to content

Result of Regex.Match("wtfb",@"(.)()+?b") is not same as new Regex(@"(.)()+?b",RegexOptions.Compiled).Match("wtfb") #111051

Closed

Description

@longxya

Description

If pattern includes ()+?, then Regex.Match(input,pattern) will match differnet new Regex(pattern ,RegexOptions.Compiled).Match(input)

Some discussions in GitHub Discussion #110976

Reproduction Steps

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.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Jan 3, 2025
  2. dotnet-policy-service commented on Jan 3, 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.

  3. DL444 commented on Jan 3, 2025

    @DL444

    Interpreted: b

    Typo in the expected behavior. It is expected that the interpreted result matches compiled.

  4. steveharter commented on Jan 14, 2025

    @steveharter
    Contributor

    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

  5. added this to the 10.0.0 milestone on Jan 14, 2025
  6. added
    help wanted
    on Jan 14, 2025
  7. removed
    untriaged
    on Jan 14, 2025
  8. longxya commented on Jan 14, 2025

    @longxya
    Author

    Do I need to submit new issues for other regular expression problems that appear to be bugs? Such as:

    Using new Regex("(.)(?'2-1'(?'-1'.))", RegexOptions.Compiled).Matches("wtf") will throw an exception "Index was outside the bounds of the array".

    • For regularexpression (?'2-1'(?'1'.)) , the interpreted mode has a different result than both compiled and generated via [GeneratedRegex].
  9. ovidiucosteanet commented on Jan 16, 2025

    @ovidiucosteanet
    Contributor

    Hello! I'd like to create a PR for this. Can you please assign the issue to me?

  10. removed their assignment
    on Jan 27, 2025
  11. stephentoub commented on Feb 4, 2025

    @stephentoub
    Member

    @ovidiucosteanet, if you'd like to give it a go, we'd welcome the help. Thanks.

  12. longxya commented on Mar 25, 2025

    @longxya
    Author

    @stephentoub @ovidiucosteanet
    Similar regular expressions can cause infinite matching

    Maybe it is related to this issue

    using System;
    using System.Text.RegularExpressions;
    
    string pattern = @"(()+?){2}";
    //pattern = @"(()+?)+";
    //pattern = @"(()+?)*";
    //pattern = @"(()+?){1,2}";
    string input = "WTF123a1";
    int timeOut = 10;
    Regex regex = new Regex(pattern, RegexOptions.None, TimeSpan.FromMilliseconds(timeOut));
    var mhes = regex.Matches(input);
    try
    {
    	//var matchCount = mhes.Count;// will throw TimeOutException if memory is enough
    	for (var i = 0; i < 1000; i++)
    	{
    		Console.WriteLine(mhes[i].Index + " , " + mhes[i].Length);
    	}
    }catch(Exception e)
    {
    	Console.WriteLine("Interpreted : "+e.Message);
    }

    Output:

    0 , 3
    0 , 3
    0 , 3
    0 , 3
    0 , 3
    0 , 3
      ·
      ·
      ·
    

    And I do not know why changing the quantifier to something like {2,100} would directly throw an exception System.OutOfMemoryException

    using System;
    using System.Text.RegularExpressions;
    
    string pattern = @"(()+?){2,100}";
    string input = "WTF123a1";
    int timeOut = 10;
    Regex regex = new Regex(pattern, RegexOptions.None, TimeSpan.FromMilliseconds(timeOut));
    var mhes = regex.Matches(input);
    try
    {
    	for (var i = 0; i < 1000; i++)
    	{
    		Console.WriteLine(mhes[i].Index + " , " + mhes[i].Length);
    	}
    }catch(Exception e)
    {
    	Console.WriteLine("Interpreted : "+e.Message);
    }

    Output:

    Interpreted : Exception of type 'System.OutOfMemoryException' was thrown.
    
  13. 2 remaining items

  14. modified the milestones: 10.0.0, Future on Jul 26, 2025
  15. longxya commented on Oct 16, 2025

    @longxya
    Author

    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

    using System;
    using System.Text.RegularExpressions;
    
    string input = "What is the fuck! WTF";
    string pattern="WTF(?<=(.()+?)WTF)";
    var mhes = new Regex(pattern, RegexOptions.None).Matches(input);
    try
    {
    	//var matchCount = mhes.Count;// will throw TimeOutException if memory is enough
    	for (var i = 0; i < 1000; i++)
    	{
    		Console.WriteLine(mhes[i].Value);
    	}
    }catch(Exception e)
    {
    	Console.WriteLine(e.Message);
    }

    output:

    t is the fuck! 
    t is the fuck! 
    t is the fuck! 
    .
    .
    .
    
  16. longxya commented on Oct 17, 2025

    @longxya
    Author

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

    using System;
    using System.Text.RegularExpressions;
    
    string input = "anystring anyexpress1";
    string pattern="anyexpress1(?<=(.(any express|(any express)*)+?)anyexpress1)";
    //pattern="(?<=(.(any express|(any express)*)+?))";
    //pattern="anyexpress1(?<=(.(any express|(any express)*?)+?)anyexpress1)";
    //pattern="anyexpress1(?<=(.((any express)*)+?)anyexpress1)";
    var mhes = new Regex(pattern, RegexOptions.None).Matches(input);
    try
    {
    	for (var i = 0; i < 1000; i++)
    	{
    		Console.WriteLine(mhes[i].Value);
    	}
    }catch(Exception e)
    {
    	Console.WriteLine(e.Message);
    }

    output:

    string 
    string 
    string
    .
    .
    .
    

    Example2:

    using System;
    using System.Text.RegularExpressions;
    
    string pattern = @"((any express|(any express)*)+?){2}";
    //pattern = @"((any express|(any express)*)+?)+";
    //pattern = @"((any express|(any express)*)+?)*";
    //pattern = @"((any express|(any express)*)+?){1,2}";
    string input = "any express";
    int timeOut = 10;
    Regex regex = new Regex(pattern, RegexOptions.None, TimeSpan.FromMilliseconds(timeOut));
    var mhes = regex.Matches(input);
    try
    {
    	//var matchCount = mhes.Count;// will throw TimeOutException if memory is enough
    	for (var i = 0; i < 1000; i++)
    	{
    		Console.WriteLine(mhes[i].Index + " , " + mhes[i].Length);
    	}
    }catch(Exception e)
    {
    	Console.WriteLine(e.Message);
    }

    output:

    3 , 8
    3 , 8
    3 , 8
    .
    .
    .
    
  17. removed
    help wanted
    on Oct 19, 2025
  18. added
    in-pr
    on Oct 19, 2025
  19. locked and limited conversation to collaborators on Nov 19, 2025
  20. removed
    in-pr
    on Feb 4, 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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions