Reputation: 5384
I've tried dismally. I have paragraphs consisting of Runs in a RichTextBox. Each run consists of a string with none, one, or many embeded "\r\n". I need to remove all but the first "\r\n" when there are more then one together and replace the remaining "\r\n" with "| ". The Document will have many such paragraphs.
That is, given this string:
This \r\n is a \r\n\r\n Test. Or, \r\n\r\n\r\n this \r\n.
I need this string:
This | is a | Test. Or, | this | .
How can this be done using Regex (C# .Net 4.5)?
TIA (I've studied similar questions, but can't seem to get it right :()
Edit: Thanks to all who helped. For anyone coming here, in my case the correct answer is actually a combination of the answers I received. That is, both of these work correctly in my case.
Regex.Replace(s, "(\r\n)+", "| ")
Regex.Replace(s, @"(\r\n)+","| ")
Upvotes: 0
Views: 98
Reputation: 30981
I assume, you have pairs of CF/LF chars, not \
+ r
+ \
+ n
.
If this is the case, you should not use here verbatim strings
(with @
before the actual string).
Writing @"xx\r\n"
causes that such a string contains:
r
,n
.The same for regex.
If you don't believe, run the following code:
string s = @"This \r\n is a \r\n\r\n Test. Or, \r\n\r\n\r\n this \r\n.";
Console.WriteLine(s);
You should write:
string s = "This \r\n is a \r\n\r\n Test. Or, \r\n\r\n\r\n this \r\n.";
string n = Regex.Replace(s, "(\r\n)+", "|");
Console.WriteLine(n);
Upvotes: 1
Reputation: 23732
Here is a working example:
string s = @"This \r\n is a \r\n\r\n Test. Or, \r\n\r\n\r\n this \r\n.";
string n = Regex.Replace(s, @"(\\r\\n)+", "|");
Console.WriteLine(n);
Output:
This | is a | Test. Or, | this |.
the +
sign at the end of the pattern means 1 or more occurrences of the previous characters. This way it will also capture multiple \r\n\r\n
Upvotes: 2