Regex to match an exact number of floating point numbers
<p>I have a string containing several float (or integer) numbers plus random stuff at the beginning and at the end. I want a regex that matches only the string that contains at least a given number of float and report as a group the first given number of floats.</p>

<p>Example:
Let's assume the string must contain exactly 6 float numbers. Remember that each string contains random stuff at the begin and at the end so <code>$</code> and <code>^</code> cannot be used.</p>

<pre><code>5.13e12  4.5774  4.743E-1  .655E3               NO MATCH (only 4 float)
5.13e12  4.5774  4.743E-1  .655E3 1 2           MATCH (6 float -&gt; all in 1 group)
5.13e12  4.5774  4.743E-1  .655E3 1 2 3 5 6     MATCH (6 first float in a group)
</code></pre>

<p>Until now I elaborated the following</p>

<pre><code>((\s*\d*\.?\d+[Ee]?[+-]?\d*){6})
</code></pre>

<p>but this one matches also lines with less than 6 float numbers.</p>

<p><strong>EDIT:</strong> After many tests the regex closer to what I wanted is the one from @Sebastian Proske. However, when I try to match 9 floats the computer stall. Reading around seems that this is due to the fact that the regex tries to match with any of the possible combinations that can be formed using the + and the *. Do you have any idea on how to make this possible?</p>

<p>This regex with otional groups should work for you to match at least 6 numbers in each line:</p>

<pre><code>(?:[+-]?\d*\.?\d+(?:E[+-]?\d+)?(?:\s+|$)){6}
</code></pre>

<p><a href="https://regex101.com/r/valhn1/3" rel="nofollow">RegEx Demo</a></p>

python regex python-2.7