我如何获得此正则表达式的印刷版?

时间:2019-03-26 07:18:59

标签: php regex

我的php正则表达式有问题。我需要使用它进行一些评估,如果可以将此正则表达式更改为正确的位置,会容易得多。我将json_encode转换为JSON文件的最终数组。

我已经尝试了很多不同的正则表达式,但是我从未获得正确的输出。现在我得到这样的东西:

[
    [
    "234445 01:00:34.423 ineedhelpineedhelpineedhelppls",
    "234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid",
    ],
    [
    "234445 ",
    "234445 "
    ],
    [
    "01:00:34.423",
    "01:00:34.423"
    ],
    [
    "ineedhelpineedhelpineedhelppls",
    "sdfkkdkkddddddsdfsdfiieuwrepid"
    ]

]

我的正则表达式如下: preg_match_all('/(^\d+\s)(01:\w+\S\w+\S\w+)(\s.+)/im', $data, $matches)

如果这样的话真的很不错:

[
    [
    "234445 01:00:34.423 ineedhelpineedhelpineedhelppls",
    "234445",
    "01:00:34.423",
    "ineedhelpineedhelpineedhelppls"
    ],
    [
    "234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid",
    "234445 ",
    "01:00:34.423",
    "sdfkkdkkddddddsdfsdfiieuwrepid"
    ]

]

.log文件看起来像这样:

234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
234443 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid

1 个答案:

答案 0 :(得分:0)

您只需要对preg_match_all使用PREG_SET_ORDER标志:

$data = '234445 01:00:34.423 ineedhelpineedhelpineedhelppls
234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid';
preg_match_all('/(^\d+\s)(\d+:\w+\S\w+\S\w+)(\s.+)/im', $data, $matches, PREG_SET_ORDER);
print_r($matches);

输出:

Array (
  [0] => Array (
    [0] => 234445 01:00:34.423 ineedhelpineedhelpineedhelppls
    [1] => 234445
    [2] => 01:00:34.423
    [3] => ineedhelpineedhelpineedhelppls
  )
  [1] => Array (
    [0] => 234445 03:04:24.932 sdfkkdkkddddddsdfsdfiieuwrepid
    [1] => 234445
    [2] => 03:04:24.932
    [3] => sdfkkdkkddddddsdfsdfiieuwrepid
  )
)

请注意,您还需要将正则表达式中的01更改为\d+,以匹配以例如03

Demo on 3v4l.org