Regular expression to match smallest only
<p>I have a expression like <code>c.{0,2}?m</code>and a string like <code>"abcemtcmncefmf"</code>. Currently it will matches three substrings: <code>cem</code>, <code>cm</code> and <code>cefm</code> (<a href="https://regex101.com/r/qW8qM8/1" rel="nofollow noreferrer"><strong>see here</strong></a>). But I like to match only the smallest of this, in this case, <code>cm</code>.</p>

<p>My problem is that I don't have a global match support, only the first match, because I'm using MariaDB <a href="https://mariadb.com/kb/en/mariadb/regexp_substr/" rel="nofollow noreferrer"><code>REGEXP_SUBSTR()</code></a> function. My current solution is a <a href="http://pastebin.com/sD404yg8" rel="nofollow noreferrer"><strong>stored procedure</strong></a> that I created to solve my problem. But it is 10 times slower than just a regular expression for simple cases.</p>

<p>I too tried do something like: <code>(cm|c.{0,1}?m|c.{0,2}?m)</code>, but it doesn't worked because it will match first of any group patterns, instead of try one by one in all subject string.</p>

<p>I know that regular expressions (PCRE) have some <em>black magic</em> features, but I don't found nothing to solve my problem.</p>

<hr>

<ul>
<li><strong>Note</strong>: I'm yet using non-greedy pattern (<code>.{0,2}?</code>) on my current pattern;</li>
<li>The question <strong><a href="https://stackoverflow.com/questions/26102838/regular-expression-to-find-smallest-possible-match">Regular expression to find smallest possible match</a></strong> don't is my problem;</li>
</ul>