Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how to Regex.Replace "\r\n" (the actual characters, not the line break)

I've got some horrible text that I'm cleaning up using several c# regular expressions. One issue that has me stumped is there are a number of '\r\n' strings in the text, the actual characters not the line breaks.

I've tried:

content = Regex.Replace(content, "\\r\\n", "");

and:

content = Regex.Replace(content, "\r\n", "");

but neither of them work. In the end I had to use:

content = content.Replace("\\r\\n", "\r\n");

to get the project finished, but not being able to do it in a regex annoys me.

like image 366
Skrealin Avatar asked Nov 30 '10 08:11

Skrealin


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

\r, and \n have special meaning in Regex, too, so the backslash needs to be escaped. Then, these backslashes needs to be escaped for the c# string, leading to

content = Regex.Replace(content, "\\\\r\\\\n", ""); 

or

content = Regex.Replace(content, @"\\r\\n", ""); 
like image 168
Jens Avatar answered Sep 18 '22 04:09

Jens


It is a good idea to get into the habit of using a verbatim string literals (@"example") when writing regular expressions in C#. In this case you needed this:

content = Regex.Replace(content, @"\\r\\n", "\r\n");

Otherwise you have to escape each backslash twice: once to escape it in the C# string, and then a second time to escape them for the regular expression. So a single backslash would become four backslashes with a standard string literal.

like image 44
Mark Byers Avatar answered Sep 20 '22 04:09

Mark Byers