正则表达式文件(如果未传递数组)


Regex file if array is not passed

我有一个具有此结构的文件:

[19-02-2016 16:57:17.104504] [info] system done. 
 0: array(
   'ID' => 'john foo'
 )
[19-02-2016 16:57:17.110482] [info] transaction done. 
   0: array(
      'ID' => 'john foo'
   )

现在我想将文件内容解析为 json,实际上一切正常:

<?php
$file = 'test.log';
$content = array(); 
$content["trace"] = array();
$input = file_get_contents('test.log');
preg_match_all('/'[(.*)']['s]*?'[(.*?)']['s]*?(.*)['s][^'']*''ID''[ ]*=>[ ]*''(.*)''/', $input, $regs, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($regs[0]); $i++) {
    $content['trace'][] = array(
        'date'    => $regs[1][$i],
        'type'    => trim($regs[2][$i]),
        'message' => trim($regs[3][$i]),
        'ID'      => trim($regs[4][$i]),
    );
}
// return $content;
echo '<pre>'; print_r($content); echo '</pre>';  // For testing only
$content = json_encode($content);                // For testing only
echo '<pre>' . $content . '</pre>';              // For testing only

现在此代码返回以下结果:

{
"trace":[
    {
        "date":"19-02-2016 16:57:17.104504",
        "type":"info",
        "message":"system done.",
        "ID":"john foo"
    },
    {
        "date":"19-02-2016 16:57:17.110482",
        "type":"info",
        "message":"transaction done.",
        "ID":"john foo"
    }
]

}

问题是,如果我遇到例如这种情况(在文件中):

[19-02-2016 16:57:17.104504] [info] system done. 
[19-02-2016 16:57:17.110482] [info] transaction done. 
   0: array(
      'ID' => 'john foo'
   )

我没有得到任何结果,因为正则表达式失败。这是'因为第一行没有任何数组,我该如何解决这种情况?

代码:

preg_match_all(''[(.*)']['s]*?'[(.*?)']['s]*?(.*)['s.]([^'']*''ID''[ ]*=>[ ]*''(.*)'')?', $fh, $regs, PREG_PATTERN_ORDER);
        for($i = 0; $i < count($regs[0]); $i++)
        {
            var_dump($regs);
            $content['trace'][] = array(
                'date'    => $regs[1][$i],
                'type'    => trim($regs[2][$i]),
                'message' => trim($regs[3][$i]),
                'ID'      => trim($regs[4][$i]),
            );
        }

我从var_dump得到null

更新:php 代码

  preg_match_all('/'[(.*)']['s]*?'[(.*?)']['s]*?(.*)['s.]+(?:'d+[^'']*''ID''[ ]*=>[ ]*''(.*)'')?/', $input, $regs, PREG_PATTERN_ORDER);

编辑:添加了字符串的array部分是可选的,并注意'd,假设数组行将始终以数字开头,因此它也与日志中的下一行不匹配,正如@dillinger所指出的那样