Closed Serious54088 closed 1 month ago
What is the error? The generated code looks correct.
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.
Ah, you are correct. What would be the correct approach?
This is an automatic reply, confirming that your email was received. I will reply as soon as possible. Thank you!
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\"";
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:
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: