使用正则表达式从日志文件中提取数据

本文关键字:提取 数据 文件 日志 正则表达式 | 更新日期: 2023-09-27 17:50:52

我有以下logfile

    10:12:28.571 INFO INFO McEvent (ExitWaitPostProcess) Stage: 2 
    10:12:28.571 INFO McReplay:StatusEvent: 0C0000004576656E74496E666F207631260000000A000000484C4D494E464F207631C0A80A0100007F0000010000010100000000000100000046658FDC5008000000303331333030303115000000
    5761697420666F7220706F73742070726F636573735E00000045786974205761697420666F7220706F73742070726F63657373205374616765204E6F322C20592D617869732846726F6E742053696465292C202852656172204C616E65292C205043422049443A20202020202020202020202020202020
    10:12:28.571 INFO McReplay:StatusEvent verbose: event:03130001, msg:'Wait for post process',  submsg:'Exit Wait for post process Stage No2, Y-axis(Front Side), (Rear Lane),  ID:                '
    10:12:28.571 INFO INFO McEvent (ExitWaitPostProcess) Stage: 2 

我的Regex应该如何提取McReplay之后的内容,直到第一行以时间戳开始。我在文件中有几处出现,我需要它们全部出现。

我想到了类似

的东西
               new Regex(@"^('d'd:'d'd:'d'd'.'d'd'd) INFO McReplay:(.*?)('d'd:'d'd:'d'd'.'d'd'd)",
                           RegexOptions.Singleline | RegexOptions.Multiline);

似乎我对这部分(.*?)('d'd:'d'd:'d'd'.'d'd'd)有问题

使用正则表达式从日志文件中提取数据

这将查找所有出现的

     List<string> results = new List<string>();
     MatchCollection mc = Regex.Matches("yourstring", "McReplay:(.+?)[0-9]{2}:[0-9]{2}:[0-9]{2}", RegexOptions.Singleline);
     foreach (Match item in mc)
     {
        results.Add(item.Result("$1"));
     }