title: Python substitute with numbering inside patterns
body: <p>I'm trying to come up with a python script to automatically number footnotes in pandoc markdown. Given input like this:</p>
  
  <blockquote>
    <p>This is a testing document for testing purposes only.[^0] This is a testing
    document for testing purposes only. This is a testing document for testing
    purposes only.[^121][^5] This is a testing document for testing purposes
    only.</p>
    
    <p>[^0]: Footnote contents.</p>
    
    <p>[^0]: Footnote contents.</p>
    
    <p>[^0]: Footnote contents.</p>
  </blockquote>
  
  <p>It should produce output like this:</p>
  
  <blockquote>
    <p>This is a testing document for testing purposes only.[^1] This is a testing
    document for testing purposes only. This is a testing document for testing
    purposes only.[^2][^3] This is a testing document for testing purposes
    only.</p>
    
    <p>[^1]: Footnote contents.</p>
    
    <p>[^2]: Footnote contents.</p>
    
    <p>[^3]: Footnote contents.</p>
  </blockquote>
  
  <p>I've been able to make the basic functionality work, but I'm stuck on how to cover the case of two footnotes on one line. Perhaps the loops should not be line based? Or should I opt for some sort of nested loop, replacing nth occurence of a pattern (which, as I understand from <a href="https://stackoverflow.com/questions/7182546/how-to-replace-the-nth-appearance-of-a-needle-in-a-haystack-python">this</a> question is not trivial)?</p>
  
  <p>And since I'm trying to learn as much as possible from this, feel free to drop any comments or pointers for further improvements. Thanks!</p>
  
  <p>Here is the script I have so far:</p>
  
  <pre><code>import re
  from sys import argv
  
  script, first = argv
  
  i=0
  n=0
  buff = ''
  
  # open the file specified through the first argument
  f = open(first, 'rU')
  
  for line in f:
      if re.search(r'\[\^[0-9]+\]:', line):
          i += 1
          line2 = re.sub(r'\[\^[0-9]+\]:', '[^' + str(i) + ']:', line)
          buff += line2
  
      elif re.search(r'\[\^[0-9]+\]', line):
          n += 1
          line3 = re.sub(r'\[\^[0-9]+\]', '[^' + str(n) + ']', line)
          buff += line3
  
      else:
          buff += line
  
  print buff
  
  f.close()
  </code></pre>