tags:

views:

343

answers:

3

I am replacing all occurances of \n with the <BR> tag, but for some reason the text entered has many \n in a row, so I need to combine them.

Basically, if more than 1 \n occur together, replace it with just a single <BR> tag.

Can someone help me with this?

A: 

Use the following code:

str = Regex.Replace(str, @"[\r\n]+", "<br />");

It could well be faster to call the normal Replace method multiple times and not use a Regex at all, like this:

int oldLength;
do {
    oldLength = str.Length;
    str = str.Replace('\r', '\n');
    str = str.Replace("\n\n", "\n");
} while(str.Length != oldLength);

str = str.Replace("\n", "<br />");
SLaks
+5  A: 

This will replace any sequence of carriage-returns (\r) and/or linefeeds (\n) with a single <br />:

string formatted = Regex.Replace(original, @"[\r\n]+", "<br />");

If you only want to replace sequences of two or more items then the simplistic answer is to use the {2,} quantifier (which means "at least two repetitions") instead of + (which means "at least one repetition"):

string formatted = Regex.Replace(original, @"[\r\n]{2,}", "<br />");

Note that the expression above will treat the common CR+LF combination as a sequence of two items. It's probable that you'll want to treat CR+LF as a single item instead, in which case the expression becomes slightly more complicated:

string formatted =
    Regex.Replace(original, @"(?:\r\n|\r(?!\n)|(?!<\r)\n){2,}", "<br />");
LukeH
what's the \r? for?
mrblah
\r is for carriage returns. In Windows (DOS) formatted text, you'll see carriage return/line feed combinations in the form of \r\n.
Steve Wortham
would it be possible to set a minimum of 2 repeats before a replacement?
mrblah
Yes, that's what my answer was an attempt to do. \n\n+ will do it. But if you want carriage returns included then this will work instead: (\r\n)(\r\n)+
Steve Wortham
That would only work for Windows/DOS style text (requiring both). Depending on your regex engine, something like: s/(?:\r|\n|\r\n){2}/<br/>/ would work.
Bruce Alderson
@Bruce: Your regex will treat `\r\n` as a sequence of two rather than a single item. See the third regex in my answer, which handles `\r`, `\n` or `\r\n`, and treats each of them as a single item.
LukeH
A: 

Note that string.Replace() is much faster than using RegEx:

string result = oldString.Replace("\n\n","\n");
result = result .Replace("\n","<br>");
codymanix
This won't handle three newlines; you need to call it in a loop. See my answer.
SLaks