How to parse a text file with C#

后端 未结 7 1906
面向向阳花
面向向阳花 2020-12-08 14:57

By text formatting I meant something more complicated.

At first I began manually adding the 5000 lines from the text file I\'m asking this question for,into my proje

7条回答
  •  执念已碎
    2020-12-08 15:14

    Another solution, this time making use of regular expressions:

    using System.Text.RegularExpressions;
    
    ...
    
    Regex parts = new Regex(@"^\d+\t(\d+)\t.+?\t(item\\[^\t]+\.ddj)");
    
    StreamReader reader = FileInfo.OpenText("filename.txt");
    string line;
    while ((line = reader.ReadLine()) != null) {
        Match match = parts.Match(line);
        if (match.Success) {
            int number = int.Parse(match.Group(1).Value);
            string path = match.Group(2).Value;
    
            // At this point, `number` and `path` contain the values we want
            // for the current line. We can then store those values or print them,
            // or anything else we like.
        }
    }
    

    That expression's a little complex, so here it is broken down:

    ^        Start of string
    \d+      "\d" means "digit" - 0-9. The "+" means "one or more."
             So this means "one or more digits."
    \t       This matches a tab.
    (\d+)    This also matches one or more digits. This time, though, we capture it
             using brackets. This means we can access it using the Group method.
    \t       Another tab.
    .+?      "." means "anything." So "one or more of anything". In addition, it's lazy.
             This is to stop it grabbing everything in sight - it'll only grab as much
             as it needs to for the regex to work.
    \t       Another tab.
    
    (item\\[^\t]+\.ddj)
        Here's the meat. This matches: "item\.ddj"
    

提交回复
热议问题