我有一个文本文件,格式如下:
14/12/2020 12:02:50
LOG_HERE_1 XXXXX
14/12/2020 12:04:55
LOG_HERE_2 XXXXX
14/12/2020 12:10:33
LOG_HERE_3 XXXXX
我需要解析它,使用日期上的regexp (dd/mm/yyyy hh:mm:ss),但保持数组上的日期。例如:
Array(
[0] => '14/12/2020 12:02:50 LOG_HERE_1 XXXXX',
[1] => '14/12/2020 12:02:50 LOG_HERE_2 XXXXX',
[2] => '14/12/2020 12:02:50 LOG_HERE_3 XXXXX'
)
我试过了:
$array = preg_split('/(d{2}/d{2}/d{4}sd{2}[:]d{2}[:]d{2})/', $data, null, PREG_SPLIT_DELIM_CAPTURE);
但它显示了我:
{
0: "",
1: "14/12/2020 12:02:50",
2: "",
3: "14/12/2020 12:04:55",
4: "",
5: "14/12/2020 12:10:33",
6: ""
}
使用标志PREG_SPLIT_DELIM_CAPTURE
,您还可以匹配以日期时间格式开头的行,然后使用负向前看(?!
^(d{2}/d{2}/d{4}hd{2}:d{2}:d{2}b.*R(?:(?!d{2}/d{2}/d{4}hd{2}:d{2}:d{2}b).*R?)*)
如果以类似日期的模式开始也足够了,您可以将其缩短为:
^(d{2}/d{2}/d{4}b.*R(?:(?!d{2}/d{2}/d{4}b).*R?)*)
查看regex演示
例如
$pattern = "~^(d{2}/d{2}/d{4}hd{2}:d{2}:d{2}b.*R(?:(?!d{2}/d{2}/d{4}hd{2}:d{2}:d{2}b).*R?)*)~m";
$result = preg_split($pattern, $data, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
print_r($result);
查看php演示或与日志在同一行。
输出Array
(
[0] => 14/12/2020 12:02:50
LOG_HERE_1 XXXXX
[1] => 14/12/2020 12:04:55
LOG_HERE_2 XXXXX
[2] => 14/12/2020 12:10:33
LOG_HERE_3 XXXXX
)
注意日期类模式不验证日期本身。