Regular expressions in C#
-
Hi, I have an application where users can do many find/replace operations, including regexes. The problem is that if the user wants to replace something with a carriage return/newline and enter
\r\n
it literally replaces with "\r\n", however if I try the same thing but in the code, and specify a regex to replace something with\r\n
it does replace with a carriage return/newline. Otherwise the regex works properly, for example the user can find a carriage return/newline with\r\n
and they can replace with$1
for example, but it just doesn't work when you try and replace with\r\n
. thanks for any help! -
Hi, I have an application where users can do many find/replace operations, including regexes. The problem is that if the user wants to replace something with a carriage return/newline and enter
\r\n
it literally replaces with "\r\n", however if I try the same thing but in the code, and specify a regex to replace something with\r\n
it does replace with a carriage return/newline. Otherwise the regex works properly, for example the user can find a carriage return/newline with\r\n
and they can replace with$1
for example, but it just doesn't work when you try and replace with\r\n
. thanks for any help!If the user types this into the textbox it's like you would define the string like the following:
string s = @"\r\n";
orstring s = "\\r\\n";
You could replace it manually within your code:string s = s.Replace(@"\r, "\r).Replace(@"\n", "\n");
-
If the user types this into the textbox it's like you would define the string like the following:
string s = @"\r\n";
orstring s = "\\r\\n";
You could replace it manually within your code:string s = s.Replace(@"\r, "\r).Replace(@"\n", "\n");