import re
regex = re.compile(r"(?|(\[[\"]?.+?[\"]?\])(=>)\R\s*|(?<=\(0\) )(\{)\R\s*(\}))")
test_str = ("object(Symfony\\Component\\HttpFoundation\\Request)#250 (21) {\n"
" [\"attributes\"]=>\n"
" object(Symfony\\Component\\HttpFoundation\\ParameterBag)#253 (1) {\n"
" [\"parameters\":protected]=>\n"
" array(5) {\n"
" [\"_controller\"]=>\n"
" object(Closure)#239 (2) {\n"
" [\"static\"]=>\n"
" array(1) {\n"
" [\"app\"]=>\n"
" object(Silex\\Application)#14 (8) {\n"
" [\"providers\":protected]=>\n"
" array(5) {\n"
" [0]=>\n"
" object(Silex\\Provider\\HttpKernelServiceProvider)#17 (0) {\n"
" }\n"
" [1]=>\n"
" object(Silex\\Provider\\RoutingServiceProvider)#26 (0) {\n"
" }\n"
" [2]=>\n"
" object(Silex\\Provider\\ExceptionHandlerServiceProvider)#36 (0) {\n"
" }\n"
" [3]=>\n"
" object(Silex\\Provider\\TwigServiceProvider)#38 (0) {\n"
" }\n"
" [4]=>\n"
" object(Euskadi31\\Silex\\Provider\\CorsServiceProvider)#54 (0) {\n"
" }\n"
" }\n"
" [\"booted\":protected]=>\n"
" bool(true)\n"
" [\"values\":\"Pimple\\Container\":private]=>\n"
" array(37) {\n"
" [\"request.http_port\"]=>\n"
" int(80)\n"
" [\"request.https_port\"]=>\n"
" int(443)\n"
" [\"debug\"]=>\n"
" bool(true)\n"
" [\"charset\"]=>\n"
" string(5) \"UTF-8\"\n"
" [\"logger\"]=>\n"
" NULL\n"
" [\"resolver\"]=>\n"
" object(Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver)#307 (1) {\n"
" [\"logger\":\"Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver\":private]=>\n"
" NULL\n"
" }\n"
" [\"argument_metadata_factory\"]=>\n"
" object(Symfony\\Component\\HttpKernel\\ControllerMetadata\\ArgumentMetadataFactory)#301 (0) {\n"
" }\n"
" [\"argument_value_resolvers\"]=>\n"
" array(5) {\n"
" [0]=>\n"
" object(Silex\\AppArgumentValueResolver)#298 (1) {\n"
" [\"app\":\"Silex\\AppArgumentValueResolver\":private]=>\n"
" *RECURSION*\n"
" }\n"
" [1]=>\n"
" object(Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\RequestAttributeValueResolver)#295 (0) {\n"
" }\n"
" [2]=>\n"
" object(Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\RequestValueResolver)#292 (0) {\n"
" }\n"
" [3]=>\n"
" object(Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\DefaultValueResolver)#289 (0) {\n"
" }\n"
" [4]=>\n"
" object(Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\VariadicValueResolver)#286 (0) {\n"
" }\n"
" }")
subst = "\\1 \\2"
result = regex.sub(subst, test_str)
if result:
print(result)
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 Python, please visit: https://docs.python.org/3/library/re.html