title: How to count instances of a string and replace them with another string + the current counter?
body: <p><strong>Apology:</strong> I am new to programming. I honestly tried hard to make it work. I think that I understand what the problem is, but not how to solve it. I used some answered questions on this forum in my code, but it wasn't enough.</p>
  
  <p><strong>Initial point:</strong> I have a txt-file. In this txt-file some lines contain a specfic string, '<code>&lt;lb n=""/&gt;</code>', while others do not.
  Take this for example</p>
  
  <pre><code>&lt;lb n=""/&gt;magna quaestio
  &lt;lb n=""/&gt;facile solution
  &lt;pb n="5"/&gt;
  &lt;lb n=""/&gt;amica responsum
  </code></pre>
  
  <p><strong>Goal:</strong> I want to count the string <code>&lt;lb n=""/&gt;</code> line per line and fill in the current counter into the string.</p>
  
  <p>So after running the script the example should look like this:</p>
  
  <pre><code>&lt;lb n="1"/&gt;magna quaestio
  &lt;lb n="2"/&gt;facile solution
  &lt;pb n="5"/&gt;
  &lt;lb n="3"/&gt;amica responsum
  </code></pre>
  
  <p>Below is the relevant part of my script.</p>
  
  <p><strong>Problem:</strong> When using my script, every string gets replaced with the total counter <code>&lt;lb n="464"&gt;</code> instead of the current one.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>def replace_text(text):
      lines = text.split("\n")
      i = 0
      for line in lines:
          exp1 = re.compile(r'&lt;lb n=""/&gt;')                            # look for string
          if '&lt;lb n=""/&gt;' in line:                                    # if string in line
              text1 = exp1.sub('&lt;lb n="{}"/&gt;'.format(i), text)        # replace with lb-counter
              i += 1
      return text1
  </code></pre>
  
  <p>Can you please tell me how to solve my problem? Was I even on the right track with my script?</p>