簡體   English   中英

僅替換一個換行符替換多個換行符

[英]Replacing more than one line breaks by only one line break

我想在C#中只用一個\\r\\n\\r\\n替換任意數量的\\r\\n 對不起,如果這是一個愚蠢的問題,但我是regex的新手。
其實我試過了

clearstring = Regex.Replace(clearstring, @"\r\n+", "\r\n\r\n", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);

但它沒有用,有什么建議嗎? 我會很感激。

嘗試

clearstring = Regex.Replace(clearstring, @"(\r\n)+", "\r\n\r\n", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);

規則是量詞(在你的情況下為加號)僅適用於前面的組或字符類,在你的情況下只有\\ n。 如果要包含多個字符或類,則應將它們分組到paranthesis中。

Regex.Replace(str, @"(\r\n){2,}", Environment.NewLine)

只有在找到兩個或多個連續的空行時才會替換它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM