C#, Parse File .txt in a Matrix
<p>I'm working on a program in C#, that takes in input, a .txt file, and return in output, a matrix.
The .txt file is like this:</p>

<pre><code>Name: 1568

    1335  78544  453543 

---+---] [---+---] [---+----(R)



=====================

Name: 456543

    13454  5345634  53533 

---+---] [---+---] [---+----(R)



=====================

Name: 245242

      2555544   55555    55686    5554545  5455   335353     5354      353353  
---+---] [----+--]/[---+--]/[---+--]/[---+--]/[---+--]/[---+--] [--+-----(R)



=====================
</code></pre>

<p>I made the program. This is the code:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;

namespace provaParseOnlyAnd
{
    public class Class1
    {
        public static void Main()
        {
            StreamReader reader = new StreamReader("C:\\Users\\Francesco\\Desktop\\tirocinio\\Nocentini\\adj1.txt"); //LETTURA DEL FILE TESTO
            string pippo = " ";
            List&lt;string&gt; listTxt = new List&lt;string&gt;(); //CREAZIONE LISTA LIST&lt;T&gt;

            while (pippo != null)
            {
                pippo = reader.ReadLine();
                if (pippo != null)                  //POPOLAZIONE DELLA LISTA
                {
                    listTxt.Add(pippo);
                }
            }
            reader.Close();

            int countEq=0;//CONTATORE NUMERO DI EQUAZIONI
            int countUguali = 0;
            foreach (String linea in listTxt)
            {
                Console.WriteLine("\n" + linea);
                foreach (char c in linea)                           //CONTEGGIO NUMERO DI : per sapere quante equazioni ho
                {
                    if (c == ':')
                        countEq++;
                }
                foreach (char c in linea)
                {
                    if(c==('='))
                        countUguali++;
                }



            }

            //Console.WriteLine("NUMERO DI UGUALI"+countUguali);
            //int numeroUguali = countUguali / countEq;
            //Console.WriteLine("NUMERO DI UGUALI TOTALI" + numeroUguali);
            Console.WriteLine("NUMERO DI EQUAZIONI "+countEq);
            Console.WriteLine("\npippo :{0}", listTxt.Exists(endWithR));
            Console.WriteLine("\npipposquare : {0}", listTxt.Exists(findSquare));
            Console.WriteLine("\npippoStart : {0}", listTxt.Exists(findStart));                   //PROVE ESISTENZA CONNETTORI ETC...
            Console.WriteLine("\npippoDylanDog : {0}", listTxt.Exists(prova));
            Console.WriteLine("\npippofindSPace :{0}", listTxt.Exists(findSpace));
            Console.WriteLine("\npippofindLink : {0}", listTxt.Exists(findLink));
            Console.WriteLine("\npippofindName : {0}", listTxt.Exists(findName));


            Console.Write("\n------------------------------------------------------\n");
            int n = listTxt.Count();
            Console.WriteLine("\n NUMERO DI RIGHE......" + n);                               //DIMENSIONE INIZIALE DELLA LIST
            Console.Write("\n------------------------------------------------------\n\n\n\n");
            int k=0;
            for(int i=0;i&lt;n;i++)
            //foreach (String linea in listTxt)
            {
                //if (linea.Equals("Name"))
                //    int i = 1;

                if (listTxt[i].Contains(":") == true)
                    listTxt.RemoveAt(i);
                //if (listTxt[i].Contains("=====================") == true)
                //    listTxt.Remove("=====================");
                //if (listTxt[i].Contains("=====================") == true)
                //{
                //    k++;
                //    if (k * countEq == countUguali)
                //        break;
                //}

                if (listTxt[i].Contains("] [") == true)
                {
                    listTxt.RemoveAt(i + 1);
                    listTxt.RemoveAt(i + 1);
                    listTxt.RemoveAt(i + 1);
                    listTxt.RemoveAt(i + 1);
                    //listTxt.RemoveAt(i + 1);
                    listTxt.Insert(i + 1, "=====================");

                }
                if (listTxt[i].Contains("=====================") == true)
                {
                    k++;
                    if (k * 21 == countUguali)
                    {
                        break;
                    }
                    else
                    {
                        listTxt.RemoveAt(i + 1);
                    }
                }


            }
           //listTxt.RemoveAt(0);                                                              //RIMOZIONE DEL NAME:XXXXXX
           //listTxt.Remove("=====================");                                            //RIMOZIONE DI ===============
                                                   //E INSERIMENTO SUBITO SOTTO ULTIMA EQUAZIONE
           //listTxt.RemoveRange(3, 3);



          //  int count=0;
            int count2=0;
            foreach (string linea in listTxt)
            {
                Console.WriteLine("\n" + linea);
               // count = linea.Split('[').Length - 1;
                foreach (char c in linea)                           //CONTEGGIO NUMERO DI [
                {
                    if (c == '[')
                        count2++;
                }

            }

          //  Console.WriteLine("IL VALORE DI COUNT E' " +count);
            Console.WriteLine("IL VALORE DI COUNT 2 E' "+count2);
            count2 = count2 + 1;             //count rappresenta il numero di "interruttori". Sommo uno perchè ricerco solo [, e quindi non conta il coil
            Console.WriteLine("IL VALORE DI COUNT 2 E' " + count2);







            Console.Write("\n\n\n\n------------------------------------------------\n");
            int n1 = listTxt.Count();
            int col = count2 * 2;                     //IL NUMERO DI COLONNE E' UGUALE
            int row = n1 - k;                       //SOTTRAGGO UNO PERCHE' L'ULTIMA RIGA CONTIENE ==========
            Console.WriteLine("\n NUMERO DI RIGHE(compreso =========)......" + n1);                          //DIMENSIONE DELL'ARRAY LIST DOPO PARSE
            Console.WriteLine("\n NUMERO DI RIGHE.........." + row);
            Console.WriteLine("\n NUMERO DI COLONNE........." + col);
            Console.Write("\n\n\n\n------------------------------------------------\n\n");



           // if (n1 == 3)                                //SE LA LISTA HA ESATTAMENTE 3 RIGHE SIGNIFICA, CHE HO SOLO AND
            //{


                int[,] tmp = new int[row, col];

                for (int i = 0; i &lt; row; i++)
                {
                    for (int j = 0; j &lt; col; j++)
                    {
                        tmp[i, j] = 0;
                    }
                }


                if (listTxt.Exists(findSquare) == true)
                {
                    for (int i = 0; i &lt; row; i++)
                    {
                        for (int j = 0; j &lt; col-1; j=j+2)
                        {
                            Random randomClass = new Random();
                            int randomNumber = randomClass.Next(50, 500);
                            if (i == 0)
                            {

                                tmp[i, j + 1] = randomNumber;
                            }
                            else
                            {
                                tmp[i, j+1] = -6;
                                //tmp[i, 3] = -6;
                            }
                        }
                    }
                }
                if (listTxt.Exists(endWithR) == true)
                {
                    tmp[1, col - 1] = -3;
                }
                if (listTxt.Exists(findStart) == true)
                {
                    tmp[0, 0] = -1;
                }
                if (listTxt.Exists(findSource) == true)
                {
                    tmp[1, 0] = -2;
                }

                if (listTxt.Exists(findSpace) == true)
                {
                    for (int i = 0; i &lt; row; i++)
                    {
                        for (int j = 1; j &lt; col - 1; j=j+2)
                        {
                            if (i == 0)
                            {
                                tmp[i, j+1 ] = -1;
                            }
                        }

                    }

                }
                if (listTxt.Exists(findLink) == true)
                {
                    for (int i = 0; i &lt; row; i++)
                    {
                        for (int j = 1; j &lt; col - 1; j = j + 2)
                        {
                            if (i == 1)
                            {
                                tmp[i, j + 1] = -9;
                            }
                        }

                    }
                    //tmp[1, 2] = -9;
                    //tmp[1, 4] = -9;
                }



                for (int riga = 0; riga &lt; row; riga++)
                {
                    if (riga != 1)
                    {
                        for (int colonna = 0; colonna &lt; col; colonna++)
                        {
                            Console.Write(tmp[riga, colonna].ToString() + "\t");
                        }
                    }
                    else
                    {
                        Console.WriteLine("\n");
                        for (int colonna = 0; colonna &lt; col; colonna++)
                        {
                            Console.Write(tmp[riga, colonna].ToString() + "\t");
                        }
                    }
                //}



                using (TextWriter tw = new StreamWriter("C:\\Users\\Francesco\\Desktop\\tirocinio\\Nocentini\\scrittura.txt"))   //SCRITTURA DELLA MATRICE SU FILE
                {
                    //for (int i = 0; i &lt; row; i++)
                    //{
                    //    for (int j = 0; j &lt; col; j++)
                    //    {

                    //        tw.Write(" ");
                    //        tw.Write(tmp[i, j]);
                    //    }
                    //    // tw.WriteLine;
                    //}
                    for (int i = 0; i &lt; row; i++)
                    {
                        if (i != 1)
                        {
                            for (int j = 0; j &lt; col; j++)
                            {
                                tw.Write(" ");
                                tw.Write(tmp[i, j]);
                            }
                        }
                        else
                        {
                            tw.Write("\n");
                            for (int j = 0; j &lt; col; j++)
                            {
                                tw.Write(" ");
                                tw.Write(tmp[i, j]);
                            }
                        }
                    }
                }

            }




            Console.ReadLine();
        }// &lt;&lt;&lt;&lt;-----------------------------------------CHIUSURA MAIN---------------------&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;
        private static bool endWithR(string s)
        {
            return s.EndsWith("(R)");
        }
        private static bool findSquare(string s)
        {
            return s.Contains("] [");
        }
        private static bool findStart(string s)
        {

            return s.Contains("    ");
        }
        private static bool prova(string s)
        {
            return s.Contains("dylandog");
        }
        private static bool findSource(string s)
        {
            return s.Contains("---");
        }
        private static bool findSpace(string s)
        {
            return s.Contains(" ");
        }
        private static bool findLink(string s)
        {
            return s.Contains("+---");
        }
        private static bool findName(string s)
        {
            return s.Contains("Name: ");
        }
        private static bool findEquals(string s)
        {
            return s.Contains("=====================");
        }



    }
}
</code></pre>

<p>This is the ouput</p>

<pre><code>-1 154 -1 154 -1 154

-2 -6 -9 -6 -9 -3
</code></pre>

<p>It works fine. What's the problem?</p>

<p>The problem is that, it works fine only if the .txt file contains, only one of this "equation". For example, only this</p>

<p>Name: 1568</p>

<pre><code>    1335  78544  453543 

---+---] [---+---] [---+----(R)



=====================
</code></pre>

<p>If the .txt contains, two or more of that "equations", I need to have in output, n matrix x n "equation", but this not happen, he give me, only one wrong matrix.</p>

<p>If i have a file like the one I put at the beginning, my expected output should be :</p>

<pre><code>   -1 154 -1 154 -1 154

    -2 -6 -9 -6 -9 -3



    -1 321 -1 321 -1 321

    -2 -6 -9 -6 -9 -3



   -1 200 -1 200 -1 200 -1 200 -1 200 -1 200 -1 200 -1 200

   -2 -6  -9 -6  -9 -6  -9 -6  -9  -6 -9  -6 -9 -6  -9 -3
</code></pre>

<p>In substance, where i have ][, i put in the first row a random number (200, 321, 154..etc) separated by -1, in the second row, i put a code (-6), separeted by -9. Only the first column of the second row, is -2, and the last -3.</p>
