Thanks to visit codestin.com
Credit goes to github.com

Skip to content

error CS1056: Unexpected character '−' with Regex on Mac #113077

@exacs

Description

@exacs

Description

Compilation error CS1056 on Mac when using GeneratedRegex with this regular expression:

[GeneratedRegex("(?>'[^']*'|\"[^\"]*\"|\\([^()]*(((?<open>\\()[^()]*)+((?<-open>\\))[^()]*)+)*(?(open)(?!))\\)|[^'\",])+", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en")]

Reproduction Steps

On MacOS (Sonoma 14.4) with dotnet 9.0.200, create a minimal project and compile with dotnet build:

using System.Text.RegularExpressions;

namespace ConsoleApp1
{
    internal partial class Program
    {
        [GeneratedRegex("(?>'[^']*'|\"[^\"]*\"|\\([^()]*(((?<open>\\()[^()]*)+((?<-open>\\))[^()]*)+)*(?(open)(?!))\\)|[^'\",])+", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en")]
        private static partial Regex FindColumnsPattern();
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Expected behavior

The program compiles

Actual behavior

The program does not compile and got this error

ConsoleApp1 failed with 1 error(s) (0,1s)
    /Users/carlos.saito/tmp/weird-regex/ConsoleApp1/obj/Debug/net9.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs(409,78): error CS1056: Unexpected character '−'

Note: in the console message, the in Unexpected character '−' is a minus sign which is not present in the source code

Regression?

No response

Known Workarounds

Removing the - in the regex capture group compiles, but it is a different expression:

- [GeneratedRegex("(?>'[^']*'|\"[^\"]*\"|\\([^()]*(((?<open>\\()[^()]*)+((?<-open>\\))[^()]*)+)*(?(open)(?!))\\)|[^'\",])+", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en")]
+ [GeneratedRegex("(?>'[^']*'|\"[^\"]*\"|\\([^()]*(((?<open>\\()[^()]*)+((?<open>\\))[^()]*)+)*(?(open)(?!))\\)|[^'\",])+", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en")]
                                                                            ^ here

Configuration

$ dotnet --version
9.0.200
$ dotnet sdk check
.NET SDKs:
Version      Status     
------------------------
9.0.200      Up to date.

.NET Runtimes:
Name                          Version      Status     
------------------------------------------------------
Microsoft.AspNetCore.App      9.0.2        Up to date.
Microsoft.NETCore.App         9.0.2        Up to date.

Running on Mac OS 14.4

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions