Kenny Yap
Kenny Yap

Reputation: 1337

Regex - Extracting contents within two custom delimiters

Would like to check, I am very new to regular expression and I am attempting to extract contents of my custom log files within 2 custom delimiters. How should I do that? I tried preg_match('/-----BEGIN DEPLOYMENT-----(.*?)-----END DEPLOYMENT-----/s', $logFile, $extracted); but it do not seem to work properly. Below is a sample of the log file I made for my bitbucket autodeploy function.

-----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-----

Upvotes: 1

Views: 33

Answers (1)

Pedro Pinheiro
Pedro Pinheiro

Reputation: 1069

Your regex seems to work fine, but to actually capture all occurrences you need to use the preg_match_all method:

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

Upvotes: 1

Related Questions