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.

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.

In literal C# strings, as well as in
C++ and many other .NET languages, the
backslash is an escape character. The
literal string "\\" is a single
backslash. In regular expressions, the
backslash is also an escape character.
The regular expression \\ matches a
single backslash. This regular
expression as a C# string, becomes
"\\\\". That's right: 4 backslashes
to match a single one.

Note: I had to write 8 backslashes in the next to last sentence so 4 backslashes would get displayed ;-)