title: C# Text file and regular expressions
body: <p>I seem to be having a problem with the following file:</p>
  
  <pre><code>*User Type  0:        Database Administrator
  Users of this Type:
                       Database Administrator          DBA         Can Authorise:Y     Administrator:Y
                       DM3 Admin Account               DM3         Can Authorise:Y     Administrator:Y
  Permissions for these users:
  Data - Currencies                                  Parameters - Database                                Add FRA Deal                                     Reports - Confirmation Production
    Add Currency                                       Amend Database Parameters                          Cancel FRA Deal                                  Reports - System Printer Definitions
    Delete Currency                                  Parameters - Data Retention                          Amend FRA Deal                                     Save System Printers
    Amend Currency                                     Amend Data Retention Parameters                    Amend Settlements Only                           Custom Confs/Tickets
    Amend Currency Rates                             Data - Rate References                               Verify FRA Deal                                    Add Custom Confs/Tickets
    Amend Currency Holidays                            Add Rate Reference                                 Add FRA Deal (Restricted)                          Delete Custom Confs/Tickets
    Add Nostro                                         Delete Rate Reference                              Release FRA Deal                                   Amend Custom Confs/Tickets
    Amend Nostro                                       Amend Rate Reference                             Deal - IRS                                         Reports - System Report Batches
    Delete Nostro                                    Deal - Call Accounts                                 Add IRS Deal                                       Save System Batches
  Data - Currency Pairs                                Open Call Account                                  Cancel IRS Deal                                  Reports - View Reports Spooled
    Add Currency Pair                                  Amend Call Account                                 Amend IRS Deal                                   View - Audits
    Delete Currency Pair                               Close Call Account                                 Amend Settlements Only                             Print Audit
    Amend Currency Pair                                Amend Settlements Only                             Verify IRS Deal                                    Print Audit Detail
  Data - Books                                       Data - Sales Relationship Mgrs                       Add IRS Deal (Restricted)                          Filter Audit*
  </code></pre>
  
  <p>I am using a regular expression to check each line for a pattern. In total there are three patterns that need to match. If you look at the first three lines, that is all the information that needs to be taken from the file. The problem im having is that my regex is not matching. Also what needs to be done is the information needs to be taken from between two lines.... How do i do that? </p>
  
  <p>This is the code i have so far:</p>
  
  <pre><code>        string path = @"C:/User Permissions.txt";
          string t = File.ReadAllText(path);
  
          //Uses regular expression check to match the specified string pattern
          string pattern1 = @"User Type ";
          string pattern2 = @"Users of this Type:";
          string pattern3 = @"Permissions for these users:";
          Regex rgx1 = new Regex(pattern1);
          Regex rgx2 = new Regex(pattern2);
          Regex rgx3 = new Regex(pattern3);
  
          MatchCollection matches = rgx1.Matches(t);
          List&lt;string[]&gt; test = new List&lt;string[]&gt;();
  
          foreach (var match in matches)
          {
              string[] newString = match.ToString().Split(new string[] { @"User Type ", }, StringSplitOptions.RemoveEmptyEntries);
  
              for (int i = 3; i &lt;= newString.Length; i++)
              {
                  test.Add(new string[] { newString[0], newString[1], newString[i - 1] });
              }
  
          }
  
          MatchCollection matches2 = rgx2.Matches(t);
          List&lt;string[]&gt; test2 = new List&lt;string[]&gt;();
  
          foreach (var match2 in matches2)
          {
              string[] newString = match2.ToString().Split(new string[] { @"Permissions for these users: ", }, StringSplitOptions.RemoveEmptyEntries);
  
              for (int i = 3; i &lt;= newString.Length; i++)
              {
                  test2.Add(new string[] { newString[0], newString[1], newString[i - 1] });
              }
  
          }
  
          MatchCollection matches3 = rgx3.Matches(t);
          List&lt;string[]&gt; test3 = new List&lt;string[]&gt;();
  
          foreach (var match3 in matches3)
          {
              string[] newString = match3.ToString().Split(new string[] { @"Users of this Type: ", }, StringSplitOptions.RemoveEmptyEntries);
  
              for (int i = 3; i &lt;= newString.Length; i++)
              {
                  test3.Add(new string[] { newString[0], newString[1], newString[i - 1] });
              }
  
          }
          foreach (var line in test)
          {
              Console.WriteLine(line[0]);
              Console.ReadLine();
          }
          Console.ReadLine();
  </code></pre>
  
  <p>Guffa's code seems very efficient compared to mine, the only problem i'm having now is how to extract the lines between "Users of this type" and Permissions for these users". How would go about doing this? Obviously checking to see if the name begins on a new line won't help.</p>