title: Repeated regex delimiters
body: <p>I am using this code, which I picked up from the 'net somewhere, to split a line of text into &quot;words&quot;:</p>
  <pre><code>def SplitStringIntoWords(inputString, delimiters=' \t,'):
      words = re.split('[' + delimiters + ']', inputString.strip())
      return len(words), words
  </code></pre>
  <p>However, I just noticed that it is not behaving well with multiple internal spaces (or tabs).</p>
  <pre><code>    numWords, words = SplitStringIntoWords('A B')
      print(str(numWords) + ' words')
      print(*words, sep=' ')
  
      numWords, words = SplitStringIntoWords('A  B')
      print(str(numWords) + ' words')
      print(*words, sep=' ')
  
      numWords, words = SplitStringIntoWords('A   B')
      print(str(numWords) + ' words')
      print(*words, sep=' ')
  </code></pre>
  <p>gives</p>
  <blockquote>
  <p>2 words
  A B</p>
  <p>3 words
  A  B</p>
  <p>4 words
  A   B</p>
  </blockquote>
  <p>What's the best way to collapse multiple delimiters (spaces in this case) which are internal to the string?</p>
  <p>I really only want to do it for the first X &quot;words&quot;, as everything beyond word 5 is free format text, where I suppose I ought not to collapse multiple spaces.  But that would be &quot;nice to have&quot;, not &quot;must have&quot;.</p>