C # Regex.Replace Multiple Newline Lines

I have a text file containing more or less paragraphs. Text is not words, its data is comma delimited; but it is not very important. The text file is divided into sections; there may be sections and subsections. Partitioning is indicated by more than one new line and subsections by a new line.

Thus, the sample data:

This is the, start of a, section
908690,246246246,246246
246246,246,246246

This is, the next, section,
sfhklj,sfhjk,4626246
4yw2,fdhds5juj,53ujj

Thus, the above data contains two sections, each of which contains three subsections. Sometimes, however, there are several blank lines between sections. When this happens, I want to convert several newlines, for example \n\n\n\n, only \n\n; I think regex is probably the way to do this. I may also need to use different newline standards, unix \nand windows \r\n. I think the files probably contain several endline encodings.

Here is the regex that I came up with; its nothing special:

Regex.Replace(input, @"([\r\n|\n]{2,})", Enviroment.NewLine + Enviroment.NewLine}

First off, is this a good regex solution? I'm not so good with regex.

Secondly, I want to split each section into an element in an array of strings:

Regex.Split(input, Enviroment.NewLine + Enviroment.NewLine)

Is there a way to combine these steps?

+5
2

[\r\n|\n] . , \r, \n |.

: (?:\r\n|[\r\n]) (?:\n|\r\n?). \r\n (DOS/Windows), \r ( Macintosh) \n (Unix/Linux/Mac OS X).

\n, :

Regex.Split(Regex.Replace(source, @"(?:\r\n|[\r\n])", "\n"), @"\n{2,}")
+6

String.Split , , . , . List<string> , , AddRange .

0

All Articles