regex that shouldn't work works
<p>im starting to learn regex and i tried to make a simple regex that doesn't make sense to match but it matched any way
i tried in python</p>

<pre><code>import re


pattern = r'[a-z]+[a-z]'



print re.findall(pattern,"adasdasad");
</code></pre>

<p>it returned ['adasdasad']
where it shouldn't have worked because [a-z]+ should have consumed the whole string and the rest ([a-z]) won't find any thing to conusme why it evaluates ????</p>

<p>The <code>+</code> is not a possessive quantifier and allows backtracking into the quantified subpattern.</p>

<p>The <code>[a-z]+</code> matches <code>adasdasa</code> and <code>[a-z]</code> matches <code>d</code>, see <a href="https://regex101.com/r/fG5rS2/1">this demo</a>.</p>

<p><a href="http://i.stack.imgur.com/YFsOf.png"><img src="http://i.stack.imgur.com/YFsOf.png" alt="enter image description here"></a></p>

<p>BTW, if you used <code>[a-z]++[a-z]</code> with PCRE (a pattern with a possessive quantifier <code>++</code>), it would never match anything as it would require to match 1 or more letters and then another letter that would be already consumed with the first subpattern. So, that is the same as <code>(?!)</code> pattern.</p>

python regex