$re = '/(?(DEFINE)
(?<groupStart> <g )
(?<groupAttrs> >|[^>]+> )
(?<groupStartContainer> (?:(?&groupStart)(?&groupAttrs)(?&space)){1,} )
(?<groupEnd> (?:<\/g>(?&space)){1,} )
(?<group> (?:(?&groupStartContainer)(?&tags)){1,}(?&groupEnd)(?:(?&space)(?&tags)(?&groupEnd))? )
(?<tags> (?:<[^>]+\/>(?&space)){1,} )
(?<space> (?:\s+)? )
)
(?<groupeSimples> (?&group) )/x';
$str = '<g id="Tie2"> <path fill="#402A23" d="M21,2.307c0,1.809,0,3.016,0,3.016s0.057,1.809-1.752,1.809l0.178,0.682l-5.027,5.042l1.342-5.723 c0,0-1.741,0-1.741-1.809c0-1.809,0-3.016,0-3.016S13.876,1,15.686,1s3.618,0,3.618,0S21,0.498,21,2.307z"/> <polygon fill="#402A23" points="20.993,14.717 12.001,23.709 13.684,16.14 19.919,9.905 "/> <polygon fill="#402A23" points="22.855,23.106 14.305,31.657 10.861,28.841 11.271,26.996 21.457,16.81 "/> <polygon fill="#402A23" points="24.128,28.841 17.495,34.269 15.709,32.809 23.32,25.198 "/> </g> <path id="TV" fill="#402A23" d="M32.154,8.083H17.152l4.972-5.813l-1.649-1.406l-5.688,6.633L9.098,0.863L7.447,2.27l4.972,5.813 c-5.297,0-9.574,0-9.574,0s-2.171,0-2.171,2.171v19.541c0,0,0,2.17,2.171,2.17h3.257v2.172h3.257v-2.172h16.283v2.172h3.257v-2.172 c1.997,0,3.256,0,3.256,0s2.172,0,2.172-2.17V10.254C34.326,10.254,34.326,8.083,32.154,8.083z M23.47,26.538 c0,0,0,2.171-2.171,2.171s-15.197,0-15.197,0s-2.171,0-2.171-2.171c0-2.172,0-13.027,0-13.027s0-2.171,2.171-2.171 s15.197,0,15.197,0s2.171,0,2.171,2.171C23.47,15.682,23.47,26.538,23.47,26.538z M28.355,25.452c-0.901,0-1.629-0.728-1.629-1.629 c0-0.9,0.728-1.628,1.629-1.628s1.628,0.728,1.628,1.628C29.983,24.725,29.257,25.452,28.355,25.452z M31.069,20.023h-5.428v-2.17 h5.428V20.023z M31.069,15.682h-5.428v-2.171h5.428V15.682z"/> <g id="Typewriter"> <g> <path fill="#402A23" d="M26.328,12.455v3.783H8.672v-3.783H4.888v16.396h25.223V12.455H26.328z M18.762,17.5h2.521v2.522h-2.521 V17.5z M18.762,21.283h2.521v2.522h-2.521V21.283z M13.717,17.5h2.522v2.522h-2.522V17.5z M13.717,21.283h2.522v2.522h-2.522 V21.283z M8.672,17.5h2.522v2.522H8.672V17.5z M8.672,21.283h2.522v2.522H8.672V21.283z M23.807,27.169H11.194v-2.102h12.612 V27.169z M26.328,23.806h-2.521v-2.522h2.521V23.806z M26.328,20.022h-2.521V17.5h2.521V20.022z"/> </g> <rect x="9.933" y="6.149" fill="#402A23" width="15.133" height="8.828"/> <rect x="1.105" y="11.194" fill="#402A23" width="2.522" height="5.044"/> <rect x="31.373" y="11.194" fill="#402A23" width="2.521" height="5.044"/> </g> <g id="capa"> <path fill="#402A23" d="M27.873,10.156L6.395,9.743c-3.322-0.064-4.033,2.12-4.077,4.438L2.117,24.654l2.607,0.003l0.202-10.474 C4.938,13.469,5.531,12.986,6.247,13l5.218,0.1l-0.226,11.73l1.304,0.025l0.225-11.729l3.91,0.075l-0.225,11.73l1.303,0.024 l0.226-11.729l9.123,0.175L26.88,25.13l1.303,0.025l0.226-11.713c1.101,0.351,1.275,1.467,1.255,2.52l-0.177,9.218l3.258,0.062 l0.178-9.217C32.982,12.886,30.478,10.205,27.873,10.156z"/> <rect x="6.138" y="13.012" transform="matrix(-0.9998 -0.018 0.018 -0.9998 13.2402 37.8739)" fill="#402A23" width="1.304" height="11.732"/> <rect x="8.745" y="13.06" transform="matrix(-0.9998 -0.0195 0.0195 -0.9998 18.424 38.033)" fill="#402A23" width="1.303" height="11.733"/> <rect x="13.959" y="13.161" transform="matrix(-0.9998 -0.0195 0.0195 -0.9998 28.8478 38.3347)" fill="#402A23" width="1.303" height="11.732"/> <rect x="19.172" y="13.262" transform="matrix(-0.9998 -0.0187 0.0187 -0.9998 39.2856 38.623)" fill="#402A23" width="1.303" height="11.731"/> <rect x="24.385" y="13.361" transform="matrix(-0.9998 -0.0195 0.0195 -0.9998 49.6941 38.9389)" fill="#402A23" width="1.304" height="11.733"/> <rect x="21.778" y="13.311" transform="matrix(-0.9998 -0.0195 0.0195 -0.9998 44.4827 38.7881)" fill="#402A23" width="1.304" height="11.733"/> </g> <g id="Typewriter2"> <rect x="9.933" y="6.149" fill="#402A23" width="15.133" height="8.828"/> <rect x="1.105" y="11.194" fill="#402A23" width="2.522" height="5.044"/> <rect x="31.373" y="11.194" fill="#402A23" width="2.521" height="5.044"/> <g> <path fill="#402A23" d="M26.328,12.455v3.783H8.672v-3.783H4.888v16.396h25.223V12.455H26.328z M18.762,17.5h2.521v2.522h-2.521 V17.5z M18.762,21.283h2.521v2.522h-2.521V21.283z M13.717,17.5h2.522v2.522h-2.522V17.5z M13.717,21.283h2.522v2.522h-2.522 V21.283z M8.672,17.5h2.522v2.522H8.672V17.5z M8.672,21.283h2.522v2.522H8.672V21.283z M23.807,27.169H11.194v-2.102h12.612 V27.169z M26.328,23.806h-2.521v-2.522h2.521V23.806z M26.328,20.022h-2.521V17.5h2.521V20.022z"/> </g> </g>
';
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