开发者

C# formatting regex problem

开发者 https://www.devze.com 2022-12-17 21:53 出处:网络
What I have is a config file with 100\'s o开发者_开发百科f lines that has the following format:

What I have is a config file with 100's o开发者_开发百科f lines that has the following format:

Input line : FHF02030304|C:\sd\dad\qwe\re|{203-207}.TXT|5`

The format is : ID|Directory|Text|# txts

The formula for the additional lines is Text (in example 203) +1 -1.

so in the following example 203 +1 -1 = 203 (the first file) 203+2-1 = 204 (the 2nd file) I need to translate that to Example output:

FHF02030304|C:\sd\dad\qwe\re|203.txt

FHF02030305|C:\sd\dad\qwe\re|204.txt

FHF02030306|C:\sd\dad\qwe\re|205.txt

FHF02030307|C:\sd\dad\qwe\re|206.txt

FHF02030308|C:\sd\dad\qwe\re|207.txt

So i basically have to translate the one line of the file to additional lines for each # of files.

What i'm trying to do is take a config file and turn it into a full file path. so in the example FHF02030304|C:\sd\dad\qwe\re|203.txt

FHF02030305|C:\sd\dad\qwe\re|204.txt

FHF02030306|C:\sd\dad\qwe\re|205.txt

FHF02030307|C:\sd\dad\qwe\re|206.txt

FHF02030308|C:\sd\dad\qwe\re|207.txt

in other words C:\sd\dad\qwe\re\203.txt is the full path. The reason the last number after the last | is so important is thats how many files are related to that file. So if there was |200 that would mean there is 200 files and i need to start after the first number whatever, and go from # +199 (and it's plus 199 because the formula is number +1 -1).


This code in C#

public void Run()
{
    string filename = "MikeD.txt";
    using (var tr = new StreamReader(filename))
    {
        string line = null;
        while ((line= tr.ReadLine()) != null)
        {
            System.Console.WriteLine("#orig: {0}",line);
            var tokens = line.Split('|');
            if (tokens.Length == 4)
            {
                // find first numeric digit in tokens[0]
                int n=0;
                while(tokens[0][n]<'0' || tokens[0][n]>'9') n++;

                // get the base for the first output argument
                int b1 = Int32.Parse(tokens[0].Substring(n));

                // get the prefix for the first output arg
                string prefix = tokens[0].Substring(0,n);

                // find the beginning index in tokens[2]
                var p1 = tokens[2].Substring(1).Split('-');
                int b2 = Int32.Parse(p1[0]);

                // find the extension in tokens[2]
                var p2 = tokens[2].Split('.');
                string ext = p2[1];

                // determine how many lines to output
                int x = Int32.Parse(tokens[3]);

                // output the lines
                for (int i=0; i < x; i++)
                {
                    System.Console.WriteLine("{0}{1}|{2}|{3}.{4}",
                                             prefix,
                                             b1+i,
                                             tokens[1],
                                             b2+i,
                                             ext
                                             );
                }

            }
            else
            {
                System.Console.WriteLine("-bad input-");
            }
        }
    }
}

Result:

#orig: FHF02030304|C:\sd\dad\qwe\re|{203-207}.TXT|5
FHF2030304|C:\sd\dad\qwe\re|203.TXT
FHF2030305|C:\sd\dad\qwe\re|204.TXT
FHF2030306|C:\sd\dad\qwe\re|205.TXT
FHF2030307|C:\sd\dad\qwe\re|206.TXT
FHF2030308|C:\sd\dad\qwe\re|207.TXT


This has nothing to do with regular expressions. I suggest you split the lines at '|', and then generate the additional lines in your program. If I understand the logic behind your text field correctly (your explanation still isn't clear), this would be the basic algorithm:

foreach line:
    values = line.split('|')
    text   = values[2]
    number, plus, minus = parse_text(text)
    end = start = number
    end = end + plus - minus
    for (i = start; i != end; i++)
        print values[0], values[1], i, values[3]
0

精彩评论

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