firasdib / Regex101

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

C# Code Gernerator: the substitution variable code is incorrect #2325

Closed Serious54088 closed 1 month ago

Serious54088 commented 2 months ago

Code Generator Language

C#

Question Description

Test Example: Insert a tab and a double quote at the end of a string https://regex101.com/r/um4Khe/1

The code generated by the 101 Code Generator is as follows:

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"$";
        string substitution = @"\t""";
        string input = @"abc";
        RegexOptions options = RegexOptions.Multiline | RegexOptions.Singleline;

        Regex regex = new Regex(pattern, options);
        string result = regex.Replace(input, substitution);
    }
}

Among these, this line of code is incorrect: string substitution = @"\t""";

The correct code should be: string substitution = "\t\"";

Code Snippet

Here is the js code to generate the C# code for substitution variable:

// Generator code.

// Replacement text input by the user
let repl = document.getElementById('sustitutionBox').value;

// Escape double quotes, other characters don't need to be modified
repl = repl.replace(/\\.|"/g, m => m === '"' ? '\\"' : m);

// Generate code for the substitution variable
let code = `string substitution = "${repl}";`;

alert(code);  // output: string substitution = "\t\"";
firasdib commented 2 months ago

What is the error? The generated code looks correct.

Serious54088 commented 2 months ago

What is the error? The generated code looks correct.

https://regex101.com/r/um4Khe/1

This provided regex example aims to append a horizontal tab character (\t) and a double quote (") to the end of the string "abc".

The correct result is obtained: abc " (a total of 5 characters).

While the 101 regex tester correctly produces the desired output abc " , the generated C# code fails to achieve the same result.

Here's the generated C# code, which you can paste into Online C# Compiler (Editor) to test.

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"$";
        string substitution = @"\t""";
        string input = @"abc";
        RegexOptions options = RegexOptions.Multiline;

        Regex regex = new Regex(pattern, options);
        string result = regex.Replace(input, substitution);
        // add lines to print the result
        Console.WriteLine($"output: {result}");
        Console.WriteLine($"length: {result.Length} characters");
    }
}

This code outputs:

output: abc\t"
length: 6 characters

That means the final result is abc followed by \ , t and ", totaling 6 characters! This is not the result we expected.

firasdib commented 1 month ago

Ah, you are correct. What would be the correct approach?

Serious54088 commented 1 month ago

This is an automatic reply, confirming that your email was received. I will reply as soon as possible. Thank you!

Serious54088 commented 1 month ago

Ah, you are correct. What would be the correct approach?

Here it is for your reference:

// How to generate the code `string substitution = xxx;`

// "replacement text" received from the user
let repl = document.getElementById('sustitutionBox').value;

// escape double quotes with a backslash, leaving other characters unchanged
let literal = repl.replace(/\\.|"/g, m => m === '"' ? '\\"' : m);

// the generated code we expect
let code = `string substitution = "${literal}";`;

alert(code);  // output: string substitution = "\t\"";
firasdib commented 1 month ago

Dup of https://github.com/firasdib/Regex101/issues/2190