C#如何Regex.Replace" \ r \ N'QUOT; (实际字符,而不是换行) [英] C# how to Regex.Replace "\r\n" (the actual characters, not the line break)
问题描述
我有说我清理使用几个C#经常EX pressions一些可怕的文字。已经难倒我的一个问题是,有许多的\ r \ N'字符串文本,实际字符不换行。
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.
我已经试过:
content = Regex.Replace(content, "\\r\\n", "");
和
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.
推荐答案
\ r
和 \ñ
有特殊含义的正则表达式,也因此反斜杠需要转义。然后,将这些反斜杠需要转义为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", "");
或
content = Regex.Replace(content, @"\\r\\n", "");
这篇关于C#如何Regex.Replace" \ r \ N'QUOT; (实际字符,而不是换行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!