我需要帮助将php + regex从输入文本文件拆分为数组: 使用preg_match_all
/(SUNDAY|MONDAY|TUESDAY|WEDNESDAY|THURSDAY|FRIDAY|SATURDAY)\R(\d{4})-([0-9]|0[1-9]|[1-2][0-9])-([0-9]|0[1-9]|[1-2][0-9]|3[0-1])\R(.*?)\R(.*?)\R\R/mi
天 - >全天议程,如下:
$x = array(
'2017-03-21' => "9:00 Meeting\n12:00 Lunch",
'2017-03-27' => "11:00 Meeting"
)
输入文件:
some of text
MONDAY
2017-03-21
9:00 Meeting
12:00 Lunch
FRIDAY
2017-03-27
11:00 Meeting
END
more text
答案 0 :(得分:1)
答案 1 :(得分:0)
通过这个正则表达式:
(?<date>\d{4}-\d{2}-\d{2})\n(?<todos>[\s\S]*?)\n\n
您在&#39;日期&#39;中捕捉日期? (匹配[1])组和其他待办事项文本&#39; todo&#39;(匹配[2])
答案 2 :(得分:0)
使用以下方法(preg_match_all
和array_combine
函数):
$re = '/\b[A-Z]+\s(\d{4}-\d{2}-\d{1,2})\s(.+?)(?=\n\n|$)/s';
$result = [];
preg_match_all($re, $str, $matches);
if (isset($matches[1]) && isset($matches[2])) {
$result = array_combine($matches[1], $matches[2]);
}
print_r($result);
输出:
Array
(
[2017-03-21] => 9:00 Meeting
12:00 Lunch
[2017-03-27] => 11:00 Meeting
)