tags:

views:

55

answers:

2

I have a file that is supposed to be \n\n delimited, but of course its not. Some of the lines contains spaces after the \n\n. How do I find a remove all spaces after a \n\n that starts a new line but that is before any other character.

Sample:

\n\nData,Mo re,Data
\n\n    Some,Li st,Of
\n\n\nOther,St uff
\n\n\n\n  This is another 

Desired Output

\n\nData,Mo re,Data
\n\nSome,Li st,Of
\n\nOther,St uff
\n\nThis is another 

Regex is probably the answer, but I'm still learning regex. Here's more or less what I've come up with Regex.Replace(input,"^(\n{2,}\s*)", "\n\n") but it doesn't work.

Edit: I should note that I pre-convert from various different line break encodings to \n before this code is needed.

+2  A: 

The backslash character needs escaping. Try:

 Regex.Replace(input,"^(\n{2,}\\s*)", "\n\n")

Also, you should consider changing \\s* to \\s+ so you don't replace valid line starts unnecesarily.

klausbyskov
This might be the problem. But then the OP's code wouldn't even have compiled since the escape sequence `\s` is not recognized inside a non-verbatim string, so I wonder..
Ani
I think the OP needs to retain the \\s* because of the desired output bases on the given input. Notice that the OP also want \n\n\n replaced with \n\n.
Les
A: 
        string test = "\n\nData,Mo re,Data \r\n\n\n    Some,Li st,Of \r\n\n\n\nOther,St uff \r\n\n\n\n\n  This is another  \r\n";
        string pattern = "^\n{2,}\\s*";
        string result = Regex.Replace(test, pattern, "\n\n", RegexOptions.Multiline);

First, you need the Multiline option. Second, how does your data really look? Notice that where you have a visible cr-lf, I put in \r\n. I did so because you said that it is a \n\n at the beginning of a line that delimits the data. The \n is confusing to work with, so be sure of your data.

Les