title: How to check if the whole input string (real numbers separated by a space) matches a regex in Python?
body: <p>I have an input string consisting of a sequence of real numbers separated by a single space. It is also acceptable for the string to contain only one real number (no spaces). My goal is to check whether the string structure matches the following (in this order):</p>
  <ul>
  <li>optional (0/1): minus (-)</li>
  <li>1/more digits</li>
  <li>optional (1+): a period and 1/more digits</li>
  <li>optional (0+): a group consisting of a space and the first group (the first three bullet points)</li>
  </ul>
  <p>It should describe the string completely. If not, it should print an error message and exit.<br />
  My current regular expression is <code>^(-?\d+(\.?\d)*)( \1)*$</code> which I thought would be okay, but even the first group doesn't match all the real numbers individually. And I need it to check the string from the beginning to the end, including the spaces.</p>
  <p>My code for this function looks like this:</p>
  <pre><code>import re
  def structure_check(string):
      structure = r&quot;^(-?\d+(\.?\d)*)( \1)*$&quot;
      if re.match(structure,string):
          return(&quot;OK&quot;)
      else:
          print(&quot;Input error&quot;)
          exit()
  </code></pre>
  <p>It should accept strings like: <code>15 35 -45 8 -2.3 4564.18 56</code> etc., but it doesn't correspond to changes in the input (doesn't match) at all. It shouldn't match if there is too many spaces, incorrectly placed <code>.</code> or <code>-</code>, or if there are other characters than digits, periods, dashes (<code>-</code>) and spaces.</p>
  <p>I could also do this with just the first group while iterating over a list created by splitting the input string by space, but I would prefer to check it according to my main goal, since I wouldn't have to split the input in the validation function and also to save some more code lines by checking the input alltogether (eg. for excess spaces, or unsupported characters, which I'd have to otherwise check separately).</p>
  <p>Sorry if I missed any answered questions, I couldn't find any appropriate for my problem in Python. If you know about any, feel free to link them, please. And thank you, I am a beginner and started learning regex for a project just about yesterday.</p>