Skip to content

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

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

Open
working-name opened this issue Jun 8, 2024 · 1 comment
Open

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

working-name opened this issue Jun 8, 2024 · 1 comment

Comments

@working-name
Copy link
Collaborator

working-name commented Jun 8, 2024

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.""""";
@doterik
Copy link

doterik commented May 8, 2025

A very good suggestion, preferably with indentation as well:

    string pattern = """

        "\w+"

        """;

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

2 participants