Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
longxia1995 opened this issue Jan 3, 2025 · 2 comments
Labels
area-System.Text.RegularExpressions untriaged New issue has not been triaged by the area owner

Comments

@longxia1995
Copy link

longxia1995 commented Jan 3, 2025

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

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 3, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

@DL444
Copy link

DL444 commented Jan 3, 2025

Interpreted: b

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.RegularExpressions untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants