Regex python findall issue
<p>From the test string:</p>

<pre><code> test=text-AB123-12a
 test=text-AB123a
</code></pre>

<p>I have to extract only <code>'AB123-12'</code> and <code>'AB123'</code>, but:</p>

<pre><code> re.findall("[A-Z]{0,9}\d{0,5}(?:-\d{0,2}a)?", test)
</code></pre>

<p>returns:</p>

<pre><code>['', '', '', '', '', '', '', 'AB123-12a', '']
</code></pre>

<p>What are all these extra empty spaces? How do I remove them?</p>

<p>Since all parts of your pattern are optional (your ranges specify <em>zero</em> to N occurences and you are qualifying the group with <code>?</code>), each position in the string counts as a match and most of those are empty matches.</p>

<p>How to prevent this from happening depends on the exact format of what you are trying to match. Are all those parts of your match really optional?</p>

python regex