title: Space padding in a CSV string using regex
body: <p>In Python, I'm trying to space-pad numbers in a CSV string with regex's.  I can of course do this with split(), format(), and join(), but my constraint is to use a regex.  Why is the following <code>re.sub</code> only doing substitutions on found matches 0,2,4,6,8? It's skipping every other match.</p>
  
  <pre><code>import re
  
  # trying to pad the single-digit numbers in a CSV with a single leading space
  # input '1,2,12,14' would produce output ' 1, 2,12,14' (notice leading spaces on single-digit numbers)
  
  s = '0,1,2,3,4,5,6,7,8,9,28,29,30,31,32,33,34,35'
  print(s)
  
  # first, add commas to front and end so that first and final numbers are captured by the regex
  s = ',' + s + ',' 
  # the main regex
  s = re.sub(r',([0-9]{1}),', r', \1,', s)
  # lastly, strip the front and end commas we added before the regex
  s = s.strip(',')
  print(s)
  </code></pre>
  
  <p>...above produces the following with input string and output string shown:</p>
  
  <pre><code>0,1,2,3,4,5,6,7,8,9,28,29,30,31,32,33,34,35
   0,1, 2,3, 4,5, 6,7, 8,9,28,29,30,31,32,33,34,35
  </code></pre>
  
  <p>So I'm just curious about the technical reason how <code>re.sub()</code> is behaving here.</p>