I'm trying to parse HTML ordered/unordered lists recursively into an OOP structure and stumbled on an issue. Let's say I have this section of code:
$text = '
<ol>
<li>
<ul>
<li>aaa</li>
<li>bbb</li>
</ul>
</li>
<li>fff</li>
<li>
<ol>
<li>ccc</li>
<li>ddd</li>
</ol>
</li>
</ol>
';
preg_match_all("/<ol>(.+?)<\/ol>/mis", $text, $matches);
The problem is that either greedy or lazy matching seem to go as shallow as possible: what I desire is the opposite, to go from deepest to shallowest, so above expression should match:
<ol>
<li>ccc</li>
<li>ddd</li>
</ol>
Any idea?
(?!.*
– Alireza Sep 26 '21 at 09:43)(.+?)<\/ol>`](https://regex101.com/r/cRxt4i/1)