2015-09-06 27 views
1

想要檢查,我是非常新的正則表達式,我試圖提取2個自定義分隔符內的自定義日誌文件的內容。我應該怎麼做?我試過preg_match('/-----BEGIN DEPLOYMENT-----(.*?)-----END DEPLOYMENT-----/s', $logFile, $extracted);,但它似乎不能正常工作。以下是我爲我的bitbucket自動部署功能所做的日誌文件示例。正則表達式 - 提取兩個自定義分隔符內的內容

-----BEGIN DEPLOYMENT----- 
2015-09-06 03:57:24-04:00 --- INFO: Attempting deployment... 
2015-09-06 03:57:24-04:00 --- INFO: Changing working directory to /home/example/status.example.com 
2015-09-06 03:57:24-04:00 --- INFO: Current work directory -> /home/example/status.example.com 
2015-09-06 03:57:24-04:00 --- INFO: Switching to master... 
2015-09-06 03:57:24-04:00 --- INFO: Active branch: 
* master 
2015-09-06 03:57:24-04:00 --- INFO: Reseting repository... HEAD is now at e529440 fix deploy controller 
2015-09-06 03:57:28-04:00 --- INFO: Pulling in changes... Already up-to-date. 
2015-09-06 03:57:28-04:00 --- INFO: Dumping autoload files... 
2015-09-06 03:57:33-04:00 --- INFO: No changes to composer.json file. installing from lock file instead... 
2015-09-06 03:57:33-04:00 --- INFO: Deployment successful. 
-----END DEPLOYMENT----- 
-----BEGIN DEPLOYMENT----- 
2015-09-06 03:58:25-04:00 --- INFO: Attempting deployment... 
2015-09-06 03:58:25-04:00 --- INFO: Changing working directory to /home/example/status.example.com 
2015-09-06 03:58:25-04:00 --- INFO: Current work directory -> /home/example/status.example.com 
2015-09-06 03:58:25-04:00 --- INFO: Switching to master... 
2015-09-06 03:58:25-04:00 --- INFO: Active branch: 
* master 
2015-09-06 03:58:25-04:00 --- INFO: Reseting repository... HEAD is now at e529440 fix deploy controller 
2015-09-06 03:58:28-04:00 --- INFO: Pulling in changes... Already up-to-date. 
2015-09-06 03:58:29-04:00 --- INFO: Dumping autoload files... 
2015-09-06 03:58:34-04:00 --- INFO: No changes to composer.json file. installing from lock file instead... 
2015-09-06 03:58:34-04:00 --- INFO: Deployment successful. 
-----END DEPLOYMENT----- 
-----BEGIN DEPLOYMENT----- 
2015-09-06 16:03:04+08:00 --- INFO: Attempting deployment... 
2015-09-06 16:03:04+08:00 --- INFO: Changing working directory to /home/example/status.example.com 
2015-09-06 16:03:04+08:00 --- INFO: Current work directory -> /home/example/status.example.com 
2015-09-06 16:03:04+08:00 --- INFO: Switching to master... 
2015-09-06 16:03:04+08:00 --- INFO: Active branch: 
* master 
2015-09-06 16:03:04+08:00 --- INFO: Reseting repository... HEAD is now at e529440 fix deploy controller 
2015-09-06 16:03:10+08:00 --- INFO: Pulling in changes... Already up-to-date. 
2015-09-06 16:03:10+08:00 --- INFO: Dumping autoload files... 
2015-09-06 16:03:15+08:00 --- INFO: No changes to composer.json file. installing from lock file instead... 
2015-09-06 16:03:15+08:00 --- INFO: Deployment successful. 
-----END DEPLOYMENT----- 
-----BEGIN DEPLOYMENT----- 
2015-09-06 16:20:23+08:00 --- INFO: Attempting deployment... 
2015-09-06 16:20:23+08:00 --- INFO: Changing working directory to /home/example/status.example.com 
2015-09-06 16:20:23+08:00 --- INFO: Current work directory -> /home/example/status.example.com 
2015-09-06 16:20:23+08:00 --- INFO: Switching to master... 
2015-09-06 16:20:23+08:00 --- INFO: Active branch: 
* master 
2015-09-06 16:20:23+08:00 --- INFO: Reseting repository... HEAD is now at e529440 fix deploy controller 
2015-09-06 16:20:27+08:00 --- INFO: Pulling in changes... Already up-to-date. 
2015-09-06 16:20:27+08:00 --- INFO: Dumping autoload files... 
2015-09-06 16:20:33+08:00 --- INFO: No changes to composer.json file. installing from lock file instead... 
2015-09-06 16:20:33+08:00 --- INFO: Deployment successful. 
-----END DEPLOYMENT----- 

回答

1

你的正則表達式看起來做工精細,但實際上捕獲所有出現,你需要使用preg_match_all方法:

preg_match_all('/-----BEGIN DEPLOYMENT-----(.*?)-----END DEPLOYMENT-----/s', $logFile, $extracted); 
print_r($extracted); 
相關問題