Regex to replace every newline
14,980
Solution 1
That will replace one or more newlines with something, not necessarily with a single newline -- that's determined by the regex_newline.Replace(...) call, which you don't show.
So basically, the answer is
Regex regex_newline = new Regex("(\r\n|\r|\n)"); // remove the '+'
// :
regex_newline.replace(somestring, "X");
Solution 2
Just use the String.Replace
method and replace the Environment.NewLine
in your string. No need for Regex.
http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx
Author by
osmiumbin
Updated on June 04, 2022Comments
-
osmiumbin almost 2 years
I need to replace every newline character in a string with another character (say 'X'). I do not wish to collapse multiple newlines with a single newline!
PS: this regex replaces all consecutive newlines with a single newline but its not what I need.
Regex regex_newline = new Regex("(\r\n|\r|\n)+");