Skip to content

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

Open
@working-name

Description

@working-name

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.""""";

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions