匹配下一个正则表达式模式后另一个模式

I want to match the NEXT occurrence of '/</\g>/' after every '/<g>/'.

I'm trying to use this in a PHP preg_replace to remove any group tags <g> that have no id. I cannot select every closing group tag because I'm not removing all group tags.

For example:

<g id="someid">
  <g>
    <!--content-->
  </g>
</g>

In the above example, the opening group tag needs to be kept, and so does the last </g> that will close the opening tag. The group tag (opening and close) in between needs to go, but not the content inside it.

Better to use DOM parser for this deletion:

$xml = '<g id="someID">
  <g>
    <path d="..." />
  </g>
</g>';
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadXML($xml); // loads your xml
$xpath = new DOMXPath($doc);
$nlist = $xpath->query("//g[not(@id)]"); // g tag without @id

$numnodes = $nlist->length;
for($i=0; $i < $numnodes; $i++) {
   $node = $nlist->item($i);
   $node->parentNode->removeChild($node);
}

$newXML =  $doc->saveXML();
echo $newXML;

OUTPUT:

<g id="someID">
</g>

It would be better to use a HTML parser instead of regex to do this kind of operation. Is there a good reason why you're using preg_replace?