Skip to content

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

Open
@longxya

Description

@longxya

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions