正则表达式 (C#):用 \r\n 替换 \n
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex (C#): Replace \n with \r\n
提问by dbkk
How can I replace lone instances of \n with \r\n (LF alone with CRLF) using a regular expression in C#?
如何使用 C# 中的正则表达式将 \n 的单独实例替换为 \r\n(单独使用 LF 与 CRLF)?
Sorry if it's a stupid question, I'm new to Regex.
对不起,如果这是一个愚蠢的问题,我是 Regex 的新手。
I know to do it using plan String.Replace
, like:
我知道使用 plan 来做String.Replace
,比如:
myStr.Replace("\n", "\r\n");
myStr.Replace("\r\r\n", "\r\n");
However, this is inelegant, and would destroy any "\r+\r\n" already in the text (although they are not likely to exist).
然而,这是不雅的,并且会破坏文本中已经存在的任何“\r+\r\n”(尽管它们不太可能存在)。
采纳答案by chakrit
Will this do?
这会吗?
[^\r]\n
Basically it matches a '\n' that is preceded with a character that is not '\r'.
基本上它匹配前面有一个不是 '\r' 的字符的 '\n'。
If you want it to detect lines that start with just a single '\n' as well, then try
如果您希望它也检测以单个 '\n' 开头的行,请尝试
([^\r]|$)\n
Which says that it should match a '\n' but only those that is the first character of a line or those that are notpreceded with '\r'
这表示它应该匹配 '\n' 但只匹配那些是行的第一个字符或前面没有'\r' 的字符
There might be special cases to check since you're messing with the definition of lines itself the '$' might not work too well. But I think you should get the idea.
可能有特殊情况需要检查,因为您弄乱了行本身的定义,“$”可能不太好用。但我认为你应该明白这一点。
EDIT:credit @Kibbee Using look-ahead s is clearly better since it won't capture the matched preceding character and should help with any edge cases as well. So here's a better regex + the code becomes:
编辑:@Kibbee 使用 look-ahead s 显然更好,因为它不会捕获匹配的前面的字符,并且应该有助于处理任何边缘情况。所以这里有一个更好的正则表达式 + 代码变为:
myStr = Regex.Replace(myStr, "(?<!\r)\n", "\r\n");
回答by neslekkiM
I guess that "myStr" is an object of type String, in that case, this is not regex. \r and \n are the equivalents for CR and LF.
我猜“myStr”是一个字符串类型的对象,在这种情况下,这不是正则表达式。\r 和 \n 是 CR 和 LF 的等价物。
My best guess is that if you know that you have an \n for EACH line, no matter what, then you first should strip out every \r. Then replace all \n with \r\n.
我最好的猜测是,如果你知道你的每一行都有一个 \n,那么你首先应该去掉每个 \r。然后用\r\n 替换所有\n。
The answer chakrit gives would also go, but then you need to use regex, but since you don't say what "myStr" is...
chakrit 给出的答案也可以,但是你需要使用正则表达式,但是因为你没有说“myStr”是什么......
Edit:looking at the other examples tells me one thing.. why do the difficult things, when you can do it easy?, Because there is regex, is not the same as "must use" :D
编辑:看其他例子告诉我一件事..为什么做困难的事情,当你可以做的时候很容易?,因为有正则表达式,与“必须使用”不同:D
Edit2: A tool is very valuable when fiddling with regex, xpath, and whatnot that gives you strange results, may I point you to: http://www.regexbuddy.com/
Edit2:当摆弄正则表达式、xpath 和诸如此类的东西会给你带来奇怪的结果时,一个工具是非常有价值的,我可以给你指出:http: //www.regexbuddy.com/
回答by Chris Marasti-Georg
myStr.Replace("([^\r])\n", "\r\n");
$ may need to be a \
$ 可能需要是 \
回答by Kibbee
It might be faster if you use this.
如果你使用它可能会更快。
(?<!\r)\n
It basically looks for any \n that is not preceded by a \r. This would most likely be faster, because in the other case, almost every letter matches [^\r], so it would capture that, and then look for the \n after that. In the example I gave, it would only stop when it found a \n, and them look before that to see if it found \r
它基本上查找前面没有 \r 的任何 \n。这很可能会更快,因为在另一种情况下,几乎每个字母都匹配 [^\r],因此它会捕获它,然后在此之后查找 \n。在我给出的例子中,它只会在找到 \n 时停止,然后他们会在此之前查看是否找到 \r
回答by wanted
I was trying to do the code below to a string and it was not working.
我试图对字符串执行下面的代码,但它不起作用。
myStr.Replace("(?<!\r)\n", "\r\n")
I used Regex.Replace and it worked
我使用了 Regex.Replace 并且它有效
Regex.Replace( oldValue, "(?<!\r)\n", "\r\n")
回答by Ico
Try this: Replace(Char.ConvertFromUtf32(13), Char.ConvertFromUtf32(10) + Char.ConvertFromUtf32(13))
试试这个: Replace(Char.ConvertFromUtf32(13), Char.ConvertFromUtf32(10) + Char.ConvertFromUtf32(13))
回答by Kevin Jin
If I know the line endings must be one of CRLF or LF, something that works for me is
如果我知道行尾必须是 CRLF 或 LF 之一,那么对我有用的是
myStr.Replace("\r?\n", "\r\n");
This essentially does the same neslekkiM's answer except it performs only one replace operation on the string rather than two. This is also compatible with Regex engines that don't support negative lookbehinds or backreferences.
这基本上与neslekkiM的答案相同,只是它只对字符串执行一个替换操作而不是两个。这也与不支持负回顾或反向引用的正则表达式引擎兼容。