Regex to match Tags NOT surrounded by Tags <p>I have the following example:</p> <pre><code>&lt;p&gt;skljklf askjas&lt;/p&gt; &lt;li&gt;dsjd sjg&lt;/li&gt; &lt;li&gt;skdkgds&lt;/li&gt; &lt;li&gt;skask las&lt;/li&gt; &lt;p&gt;skklgs aklgas&lt;/p&gt; &lt;ul&gt;&lt;li&gt;saks &lt;/li&gt;&lt;li&gt;isksa&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;asjkafsklj asjlkafs&lt;/li&gt; </code></pre> <p>As you can see there are <code>li</code>-tags which are not surrounded by an <code>ul</code>. I am trying to find a regex which selects <strong>all</strong> occurences of <code>&lt;li&gt;...&lt;/li&gt;&lt;li&gt;...&lt;/li&gt;</code> and surrounds them with <code>&lt;ul&gt;&lt;/ul&gt;</code>. So at the end I would have the following text:</p> <pre><code>&lt;p&gt;skljklf askjas&lt;/p&gt; &lt;ul&gt;&lt;li&gt;dsjd sjg&lt;/li&gt; &lt;li&gt;skdkgds&lt;/li&gt; &lt;li&gt;skask las&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;skklgs aklgas&lt;/p&gt; &lt;ul&gt;&lt;li&gt;saks &lt;/li&gt;&lt;li&gt;isksa&lt;/li&gt;&lt;/ul&gt; &lt;ul&gt;&lt;li&gt;asjkafsklj asjlkafs&lt;/li&gt;&lt;/ul&gt; </code></pre> <p>Trying it in php with <code>preg_replace</code> and <code>mb_ereg_replace</code>, but no real clue yet how I could start.</p> <p>I don't want to do DOM manipulation with <code>DOMDocument</code>.</p>