firasdib / Regex101

This repository is currently only used for issue tracking for www.regex101.com
3.2k stars 198 forks source link

.NET Code generator update from @" to """ for pattern #2292

Open working-name opened 3 weeks ago

working-name commented 3 weeks ago

franckleveque: It seems that since C# 11 (.net 7) a new way is using """ at the beginning and the end of a string to allow brut string without any escaping sequence interpretation but it is not preceded by @. However I never used it. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/

Incidental find while discussing another aspect of .NET behavior.

This regex does not produce working code:

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"""

""\w+""

""";
        string input = @"this is a 

""test""

";
        RegexOptions options = RegexOptions.Multiline;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

We should utilize """ raw string literals instead, which would make the code above output as expected:

        string pattern = """

"\w+"

""";

NOTE: the code generator will likely have to count the number of consecutive " characters inside the user's regex pattern, and simply enclose the whole shabang in +1 " characters, i.e.

string input = """""I like apples, """", and oranges.""""";