tags:

views:

58

answers:

3

I have a huge string that looks like this:

"Text
Text
Text
Text

Text
Text
Text
Text

Text
Text
Text
Text

"

Note that in between each text block is a new line. Also note that the very final text block has TWO new lines after it. So when I do a

string[] strArray = Regex.Split(content, "\r\n");

This works well, since it looks for a pattern of carriage return followed by new line. HOWEVER, for the very final entry in the array, it creates only a "" (empty string) because it does a split on the very final extra new-line.

So i want a regular expression instead of "\r\n" that will split on \r and exactly ONE \n.

any ideas?

+5  A: 

Why not do something like this?

var strArray = content.Split(new[] { "\r\n" },
    StringSplitOptions.RemoveEmptyEntries);
ChaosPandion
+1  A: 

A Regex that will match \r\n not followed by an additional \n looks like

"\r\n(?!\n)"

Does this help you? I think in your example above, the last Text will include the trailing "\r\n\n" then...

Jens
A: 

The end of your example string will be

"Text\r\nText\r\nText\r\n\r\n",

not

"Text\r\nText\r\nText\r\n\n"

as you seem to think, i.e. each line ends with "\r\n", even when the row is empty. (The confusion may stem from the fact that \r is called "cariage return" and \n is called "newline").

If you want to split a string and disregard any empty lines you could try

string[] strArray = Regex.Split(content, "(\r\n)*");
Berg