Hi I wrote a very simple C# program to use the C# Regex from command line instead of relying on the MS Word search and replace. The problem is that when I use "\r\n" as a replacement string in Regex.Replace through Console.ReadLine() it replaces with the th 4 characters "\r\n" instead of a real carriage return-newline. However, if I write string replace= "\r\n"
it works as intended, i.e. replaces the string with a carriage return-newline. An Example input string would be "Woodcock, american" (followed by \r\n). As the code is it produces "Woodstock\r\n". Here is my code:
[STAThread]
static void Main(string[] args)
{
string initial = Clipboard.GetText();
Console.Write("Find: ");
string find = Console.ReadLine();
Console.Write("Replace: ");
string replace = Console.ReadLine();
string final = Regex.Replace(initial, find, replace);
System.Threading.Thread.Sleep(3000);
Clipboard.SetText(final);
}
If you unescape
replace
(and possiblyfind
), it should do what you need.Console I/O:
MSDN Regex.Unescape