开发者

C# Text file and regular expressions

开发者 https://www.devze.com 2023-02-16 18:50 出处:网络
I seem to be having a problem with the following file: *User Type0:Database Administrator Users of this Type:

I seem to be having a problem with the following file:

*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*

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?

This is the code i have so far:

        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<string[]> test = new List<string[]>();

        foreach (var match in matches)
        {
            string[] newString = match.ToString().Split(new string[] { @"User Type ", }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 3; i <= newString.Length; i++)
            {
                test.Add(new string[] { newString[0], newString[1], newString[i - 1] });
            }

        }

        MatchCollection matches2 = rgx2.Matches(t);
        List<string[]> test2 = new List<string[]>();

        foreach (var match2 in matches2)
        {
            string[] newString = match2.ToString().Split(new string[] { @"Permissions for these users: ", }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 3; i <= newString.Length; i++)
            {
                test2.Add(new string[] { newString[0], newString[1], newString[i - 1] });
            }

        }

        MatchCollection matches3 = rgx3.Matches(t);
        List<string[]> test3 = new List<string[]>();

        foreach (var match3 in matches3)
        {
            string[] newString = match3.ToString().Split(new string[] { @"Users of this Type: ", }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 3; i <= 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();

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.


No, you are not checking each line for a pattern, you are looking for the pattern in the entire file as a single string, and you only get the exact text that matches, so when you split each result you end up with an array containing two empty strings.

If I understand correctly, each line consists of a key and a value, so there is not really any point in using regular expressions for this. Just loop through the lines and compare strings.

Here is a start:

string[] lines = @"C:/User Permissions.txt"; string t = File.ReadAllLines(path);
foreach (string line in lines) {
  if (line.StartsWith("User Type ") {
    Console.WriteLine("User type:" + line.Substring(10));
  } else if (line.StartsWith("Users of this Type:") {
    Console.WriteLine("Users:" + line.Substring(19));
  } else if (line.StartsWith("Permissions for these users:") {
    Console.WriteLine("Permissions:" + line.Substring(28));
  }
}

Edit:

Here is how to use a regular loop instead of a foreach, so that you can use an inner loop that reads lines:

string[] lines = @"C:/User Permissions.txt"; string t = File.ReadAllLines(path);
int line = 0;
while (line < lines.Length) {
  if (lines[line].StartsWith("User Type ") {
    Console.WriteLine("User type:" + lines[line].Substring(10));
  } else if (lines[line].StartsWith("Users of this Type:") {
    line++;
    while (line < lines.Length && !lines[line].StartsWith("Permissions for these users:")) {
      Console.WriteLine("User: " + lines[line]);
      line++;
    }
  } else if (lines[line].StartsWith("Permissions for these users:") {
    Console.WriteLine("Permissions:" + lines[line].Substring(28));
  }
  line++;
}


You are not going to succeed in extracting the data that you want from this txt dump using reg-exp (and hardly using any other technique without investing too much effort).

The most important obstacle to using regexp that I can see is the fact that information is actually listed in columns accross the txt file.

The problem is best illustrated with the fact that the category Data - Sales Relationship Mgrs is in one column whereas all the permissions for that category are in the next column.

Please investigate whether this information can be obtained in a different way.

Still, here is a rough algoritimic strategy for dealing with the file as is:

  1. Read the file line by line,
  2. Look at predefined offsets into the line for the information you are interested in.
  3. When you get to the information stacked in columns, you could temporarily append each column to separate collections as you parse each line
  4. Finally attempt to extract the privileges from a concatenation of all the temporary columns.
0

精彩评论

暂无评论...
验证码 换一张
取 消