$re = '/\'controllers\' => \[\s*\K(\'factories\' => )(\[((?>[^][]++|(?2))*)])/m';
$str = '<?php
namespace Application;
use Laminas\\Router\\Http\\Literal;
use Laminas\\Router\\Http\\Segment;
use Laminas\\ServiceManager\\Factory\\InvokableFactory;
return [
\'view_manager\' => [
\'template_path_stack\' => [
__DIR__ . \'/../view\',
__DIR__ . \'/../view/Application/_shared\'
],
],
\'controllers\' => [
\'factories\' => [
dsdsa
]
],
\'service_manager\' => [
\'factories\' => [
\'UsersTableGateway\' => function ($sm) {
$dbAdapter = $sm->get(\'Laminas\\Db\\Adapter\\Adapter\');
$resultSetPrototype = new \\Laminas\\Db\\ResultSet\\ResultSet();
//get base url from config
$config = $sm->get(\'Config\');
$baseUrl = $config[\'view_manager\'][\'base_url\'];
//pass base url via cnstructor to the User class
$resultSetPrototype->setArrayObjectPrototype(new Model\\Rowset\\User($baseUrl));
return new Utils\\TableGateway(\'users\', $dbAdapter, null, $resultSetPrototype);
},
\'Application\\Model\\UsersTable\' => function($sm) {
$tableGateway = $sm->get(\'UsersTableGateway\');
$table = new Model\\UsersTable($tableGateway);
return $table;
},
Utils\\Authentication::class => function($sm) {
$auth = new Utils\\Authentication(
$sm->get(\\Laminas\\Db\\Adapter\\Adapter::class),
$sm->get(Utils\\Adapter::class)
);
return $auth;
},
Utils\\Helper::class => InvokableFactory::class,
SessionManager::class => function ($container) {
$config = $container->get(\'config\');
$session = $config[\'session\'];
$sessionConfig = new $session[\'config\'][\'class\']();
$sessionConfig->setOptions($session[\'config\'][\'options\']);
$sessionManager = new Session\\SessionManager(
$sessionConfig,
new $session[\'storage\'](),
null
);
\\Laminas\\Session\\Container::setDefaultManager($sessionManager);
return $sessionManager;
},
],
],
];';
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