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.
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 ...
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.
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".
\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", "");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With