The code generator for C# removes " in the substitution string.
Reproduction steps
Lets say I have a file, containing text like this:
version: 3
version: 4
version: "5"
and I want to change the version representation to a uniform representation like
version: "3"
version: "4"
version: "5"
So I write this regex:
^version:\s*(?<version>\d+(?:\.\d+)*)$
and the following substitution:
version: "${version}"
When I test this in the Regex101 environment I get the expected results. But when I export it to C# code using the code generator, I get this code:
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^version:\s*(?<version>\d+(?:\.\d+)*)$";
string substitution = @"version: ${version}";
string input = @"version: 3
version: 4
version: 5";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
As you can see in the line where the variable substitution is declared, the string does not contain ", which causes the regex substitution to be incorrect.
Expected Outcome
The substitution variable should either be string substitution = @"version: ""${version}"""; or string substitution = "version: \"${version}\"";.
Complete code
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^version:\s*(?<version>\d+(?:\.\d+)*)$";
string substitution = @"version: ""${version}""";
string input = @"version: 3
version: 4
version: 5";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
Bug Description
The code generator for C# removes " in the substitution string.
Reproduction steps
Lets say I have a file, containing text like this:
and I want to change the version representation to a uniform representation like
So I write this regex:
and the following substitution:
When I test this in the Regex101 environment I get the expected results. But when I export it to C# code using the code generator, I get this code:
As you can see in the line where the variable
substitution
is declared, the string does not contain ", which causes the regex substitution to be incorrect.Expected Outcome
The substitution variable should either be
string substitution = @"version: ""${version}""";
orstring substitution = "version: \"${version}\"";
.Complete code
Browser
OS
Windows 11 22H2