Skip to content

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

Open
@longxya

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

Metadata

Metadata

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions