Read/Write a TXT file with timestamp I have write this code that read a txt file and re-write it whith the timestamp sorted.

This is the result (top image is input, bottom is output)


MY QUESTION IS:
Anyone do you have a suggestion to optimize this code? Because this text is a little example,my code would work with txt more longer than it. (For example 50K-60K line)

Regards

static void Main(string[] args)
{

    string[] str;            

    //Define regex string for MEX ETCS
    string messageETCS = @"(\d){4}-(\d){2}-(\d){2}.*(?:\r?\n(?!\d{4}-\d{2}-\d).*)*";
    Regex regMEXETCS = new Regex(messageETCS);

    string regexDate = @"\d{4}-\d{2}-\d{2}";
    Regex regDateETCS = new Regex(regexDate);

    // Open the text file using a stream reader.
    using (StreamReader sr = new StreamReader(@"C:\input.txt"))
    {
        // Read the stream to a string, and write the string to the console.
        String logContent = sr.ReadToEnd();

        //run regex
        List&lt;String&gt; MasterList = new List&lt;String&gt;(logContent.Length);
        Match matchResults = regMEXETCS.Match(logContent);
        Match onlyDate = regDateETCS.Match(logContent);
        str = Regex.Split(logContent, @"\d{4}-\d{2}-\d{2}", RegexOptions.None);

        while (matchResults.Success)
        {

            for (int j = 1; j &lt; str.Length;j++ ) {

            string t = str[j].TrimEnd();
            MasterList.Add(t);
            matchResults = matchResults.NextMatch();
            }
        }

        String date1 = onlyDate.ToString();
        MasterList.Sort();
        using (TextWriter tw = new StreamWriter(@"C:\output.txt"))
        {
            for (int i = 0; i &lt; MasterList.Count; i++)
            {
                tw.WriteLine(date1 + MasterList[i]);
            }
        }
    }
}
I'm quite surprised that the messageETCS regex actually works.
Probably the best and fast way it's to use a parser (ex: https://github.com/picoe/Eto.Parse).

To improve performances, read and process the file chunk by chunck, or line by line if those are lines