Skip to content
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

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

Open
Serious54088 opened this issue Aug 18, 2024 · 5 comments
Open

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

Serious54088 opened this issue Aug 18, 2024 · 5 comments

Comments

@Serious54088
Copy link

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
Copy link
Owner

What is the error? The generated code looks correct.

@Serious54088
Copy link
Author

Serious54088 commented Aug 30, 2024

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
Copy link
Owner

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

@Serious54088
Copy link
Author

Serious54088 commented Sep 14, 2024 via email

@Serious54088
Copy link
Author

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

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

3 participants