带方括号的preg_split

时间:2017-12-24 14:17:03

标签: php regex pcre preg-split

我有一个具有以下结构的日志文件:

输入

$logLine = "[2017-12-23 19:15:59:634187] [INFO] User SIMDesign successfully logged in."

我希望得到一个数组(使用preg_split或preg_match_all),如下所示:

预期输出

[
 0 => '[2017-12-23 19:15:59:634187]',
 1 => '[INFO]',
 2 => 'User SIMDesign successfully logged in.'
]

或者这个:

[
 0 => '2017-12-23 19:15:59:634187',
 1 => 'INFO',
 2 => 'User SIMDesign successfully logged in.'
]

尝试

我已经尝试了几个小时找到正则表达式模式,但不幸的是我找不到合适的解决方案。

preg_split("/[][]/", $logLine);

[
    0   =>  '',                                         //this is empty
    1   =>  '2017-12-23 19:15:59:634187',
    2   =>  ' ',                                        //there is a space
    3   =>  'INFO',
    4   =>  ' User SIMDesign successfully logged in.',
]

输出没问题但不完美。 我不需要数组的空元素([0],[2])。 我需要在字符串“用户SIMDesign成功登录”之前删除空格。

preg_split("/[][]\s\S/", $logLine);

[
    0   =>  '[2017-12-12 12:12:12:132123',
    1   =>  'INFO',
    2   =>  'ser SIMDesign successfully logged in.'     //The first character of the string was removed (why?)
]

提前致谢

1 个答案:

答案 0 :(得分:3)

首先,您需要第一组括号之间的信息,但由于这些是正则表达式的特殊字符,我们将它们转义:[(.*?)] - > \[(.*?)\]

$logLine = "[2017-12-23 19:15:59:634187] [INFO] User SIMDesign successfully logged in."
            ^--------------------------^

然后是第二个括号,相同的逻辑:[(.*?)] - > \[(.*?)\]

$logLine = "[2017-12-23 19:15:59:634187] [INFO] User SIMDesign successfully logged in."
                                         ^----^

其余的直到最后'是(.*)$

$logLine = "[2017-12-23 19:15:59:634187] [INFO] User SIMDesign successfully logged in."
                                                ^------------------------------------^

它们之间有空格(\s): \[(.*?)\]\s\[(.*?)\]\s(.*)$ neat little demo here

我更喜欢上面写的答案,但preg_split页面还记录了一些可以与preg_split()一起传递的标志,其中一个是以下内容。我不是粉丝的原因是因为你的隐藏'问题而不是修复它。

  

PREG_SPLIT_NO_EMPTY
      如果设置了此标志,preg_split()将仅返回非空片段。