$re = '@(/[^:" ()]+\..[^: "()]+):-?(\d.*?):(\d.*)?.*?((?:warning|error|note): (?:(?![\r\n]{2})[\s\S])+)@';
$str = 'Build failed: Command failed with exit code 1.
stderr: /sample/path/to/SampleFile.java:1: error: [PackageLocation] Expected package /sample/path/to/ to be declared in a directory ending with /sample/path/to, instead found /sample/path/To
package sample.path.to;
(see http://errorprone.info/bugpattern/PackageLocation)
/sample/path/to/SampleFile2.java:-1: note: Some input files use or override a deprecated API.
/sample/path/to/SampleFile2.java:-1:6 note: Recompile with -Xlint:deprecation for details.';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php