C#格式化正则表达式问题

时间:2010-01-21 17:22:56

标签: c# regex

我所拥有的是具有100行的配置文件,其格式如下:

输入行:FHF02030304 | C:\ sd \ dad \ qwe \ re | {203-207} .TXT | 5`

格式为:ID|Directory|Text|# txts

附加行的公式是Text(在示例203中)+1 -1。

所以在下面的例子中203 + 1 = 203(第一个文件) 203 + 2-1 = 204(第2档) 我需要翻译成 示例输出:
FHF02030304 | C:\ SD \爸爸\ QWE \重| 203.txt
FHF02030305 | C:\ SD \爸爸\ QWE \重| 204.txt
FHF02030306 | C:\ SD \爸爸\ QWE \重| 205.txt
FHF02030307 | C:\ SD \爸爸\ QWE \重| 206.txt
FHF02030308 | C:\ sd \ dad \ qwe \ re | 207.txt

所以我基本上必须将文件的一行翻译成每个文件的附加行。

我要做的是获取配置文件并将其转换为完整的文件路径。所以在这个例子中 FHF02030304 | C:\ SD \爸爸\ QWE \重| 203.txt
FHF02030305 | C:\ SD \爸爸\ QWE \重| 204.txt
FHF02030306 | C:\ SD \爸爸\ QWE \重| 205.txt
FHF02030307 | C:\ SD \爸爸\ QWE \重| 206.txt
FHF02030308 | C:\ SD \爸\ QWE \重新| 207.txt

换句话说C:\ sd \ dad \ qwe \ re \ 203.txt是完整路径。最后一个数字在最后一个|之后的原因非常重要的是有多少文件与该文件相关。因此,如果有| 200意味着有200个文件,我需要在第一个数字之后开始,并且从#+199开始(并且它是加上199,因为公式是+1 -1)。

2 个答案:

答案 0 :(得分:0)

这与正则表达式无关。我建议你将行拆分为“|”,然后在程序中生成其他行。如果我正确理解了你的文本字段背后的逻辑(你的解释仍然不清楚),这将是基本的算法:

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]

答案 1 :(得分:0)

此代码在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-");
            }
        }
    }
}

结果:

#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