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

BUG or Feature? Conditional expressions cannot directly contain regex options in .NET 5 and later versions #111633

Open
longxya opened this issue Jan 20, 2025 · 3 comments

Comments

@longxya
Copy link

longxya commented Jan 20, 2025

Description

(?(wtf)(?i)wtf) and (?(?i:wtf)wtf) can't run in .NET v5、v6、v7、v8、v9 cause it directly contain regex options. But it works in lower versions.

Reproduction Steps

using System;
using System.Text.RegularExpressions;

var pattern = @"(?(wtf)(?i)wtf)";
//pattern = @"(?(wtf)(?i:wtf))";// this behavior same as the expression above
var input = @"11";
try
{
	Match matchCompiled = new Regex(pattern, RegexOptions.None).Match(input);
	Console.WriteLine($"Interpreted: {matchCompiled.Success}");
}catch(Exception ex)
{
	Console.WriteLine(ex.Message);
}
try
{
	Match matchCompiled = new Regex(pattern, RegexOptions.Compiled).Match(input);
	Console.WriteLine($"Compiled: {matchCompiled.Success}");
}catch(Exception ex)
{
	Console.WriteLine(ex.Message);
}

Output:

Invalid pattern '(?(wtf)(?i)wtf)' at offset 10. Unrecognized grouping construct.
Invalid pattern '(?(wtf)(?i)wtf)' at offset 10. Unrecognized grouping construct.

Expected behavior

Output:

Interpreted: True
Compiled: True

Actual behavior

OUtput:

Invalid pattern '(?(wtf)(?i)wtf)' at offset 10. Unrecognized grouping construct.
Invalid pattern '(?(wtf)(?i)wtf)' at offset 10. Unrecognized grouping construct.

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 20, 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.

@stephentoub
Copy link
Member

stephentoub commented Jan 24, 2025

@danmoseley, this was disabled in dotnet/corefx#16609. It's not clear to me why, other than as an easy fix for addressing some parsing errors. Was it intentionally blocked off, including prohibiting quantifiers as part of the conditional's if/else expressions and not just the test expression?

@stephentoub stephentoub removed the untriaged New issue has not been triaged by the area owner label Jan 24, 2025
@stephentoub stephentoub added this to the Future milestone Jan 24, 2025
@stephentoub stephentoub removed their assignment Jan 24, 2025
@CyrusNajmabadi
Copy link
Member

If things change here, please let me know so i can keep Roslyn's regex parser up to date. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants