正则表达式 - 在两个自定义分隔符中提取内容

时间:2015-09-06 08:51:30

标签: regex

想检查,我是正则表达式的新手,我试图在2个自定义分隔符中提取自定义日志文件的内容。我该怎么办?我试过preg_match('/-----BEGIN DEPLOYMENT-----(.*?)-----END DEPLOYMENT-----/s', $logFile, $extracted);,但似乎没有正常工作。下面是我为bitbucket autodeploy函数创建的日志文件示例。

-----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 个答案:

答案 0 :(得分:1)

您的正则表达式似乎工作正常,但要实际捕获您需要使用preg_match_all方法的所有事件:

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