views:

172

answers:

7

In my program, I read in a file using this statement:

string[] allLines = File.ReadAllLines(dataFile);  

But I want to apply a Regex to the file as a whole (an example file is shown at the bottom) so I can eliminate certain stuff I'm not concerned with in the file. I can't use ReadAllText as I need to read it line by line for another purpose of the program (removing whitespace from each line).

Regex r = new Regex(@"CREATE TABLE [^\(]+\((.*)\) ON");  

(thanks to chiccodoro for this code)
This is the Regex that I want to apply.

Is there any way to change the array back into one text file? Or any other solution to the problem?
Things that pop into my mind is replacing the 'stuff' that I'm not concerned with with string.Empty.

example file

USE [Shelleys Other Database]
CREATE TABLE db.exmpcustomers(
    f_name varchar(100) NULL,
    l_name varchar(100) NULL,
    date_of_birth date NULL,
    house_number int NULL,
    street_name varchar(100) NULL
) ON [PRIMARY]
+3  A: 

You can use String.Join():

string joined = String.Join(Environment.NewLine, allLines);

If you just want to write it back to the file, you can use File.WriteAllLines() and that works with an array.

NullUserException
You can also use Environment.NewLine instead of "\r\n"
madgnome
@mag Thanks, incorporated
NullUserException
A: 

Just change from

string[] allLines = File.ReadAllLines(dataFile);

to

string allLines = File.ReadAllText(dataFile);

;)

Leo Nowaczyk
@Leo Nowaczyk: Read my question before you answer!
New Start
You also want to change the regex so that it matches newlines: new Regex(@"CREATE TABLE [^(]+\((.*)\) ON", RegexOptions.SingleLine);
siride
Not what the OP asked for
quantumSoup
yeah, right... So I think String.Join(), do the job!
Leo Nowaczyk
+5  A: 

String.Join will concatenate all the members of your array using any specified seperator.

rtalbot
@New Start: It doesn't matter if the number elements in the array can vary. The separator is the first argument (and can be the `String.Empty` if necessary).
Brian
A: 
public string CreateStringFromArray(string[] allLines)
{        
StringBuilder builder = new StringBuilder();
        foreach (string item in allLines)
        {
             builder.Append(item);
             //Appending Linebreaks
             builder.Append("\n\l");
        }
        return builder.ToString();  
}
Tokk
You need to put the newlines back in, too; ReadAllLines discards the original ones.
Alan Moore
+2  A: 

It's going to be really hard to use regexen to deal with multi-line data a line at a time. So rather than muck about with that, I'm going to suggest that you first read it as one big string, do your multi-line regex business, and then you can split it into an array of strings using String.Split (split on newlines). The reason you want to do it in this order is so that any further operations on your file data will include the changes already made by the regex. If you join the strings, then do the regex, you will either have to split that string again, or lose the changes you've made to it while you operate on the original array.

Remember to use this for your regex matching, so that it will match across newlines:

Regex r = new Regex(@"CREATE TABLE [^(]+((.*)) ON", RegexOptions.SingleLine);
siride
@siride: It's so funny, I had just finished implementing the join code and I thought to myself "God, how the hell am I gonna split it back into an array?" .. then I read this. Ha, thank you so much for your help!
New Start
Why the downvotes? I answered the actual question.
siride
+10  A: 

Hello ,

You can join string[] into a single string like this

string strmessage=string.join(",",allLines);

output :-a single , separated string.

PrateekSaluja
A: 

Could you build up a buffer as you read in each line? I have the idea that this might be a bit more efficient than getting all the lines as a string array, then joining them (...though I haven't done a full study of the issue and would be interested to hear if there is some reason that it is actually more efficient to go that way).

StringBuilder buffer = new StringBuilder();
string line = null;
using (StreamReader sr = new StreamReader(dataFile))
{
    while((line = sr.ReadLine()) != null)
    {
        // Do whatever you need to do with the individual line...
        // ...then append the line to your buffer.
        buffer.Append(line); 
    }
}
// Now, you can do whatever you need to do with the contents of
// the buffer.
string wholeText = buffer.ToString();
Pat Daburu