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>The quantifier <code>{0,n}</code> will match anywhere from 0 to n occurrences of the preceding pattern. Since the two patterns you match allow 0 occurrences, and the third is optional (<code>?</code>) it will match 0-length strings, i.e. every character in your string.</p>

<p>Editing to find a <em>minimum</em> of one and <em>maximum</em> of 9 and 5 for each pattern yields correct results:</p>

<pre><code>&gt;&gt;&gt; test='text-AB123-12a'
&gt;&gt;&gt; import re
&gt;&gt;&gt; re.findall("[A-Z]{1,9}\d{1,5}(?:-\d{0,2}a)?", test)
['AB123-12a']
</code></pre>

<p>Without further detail about <em>what exactly</em> the strings you are matching look like, I can't give a better answer.</p>

python regex