python negative look behind
<p>Is there a way to scan an entire text document and say find everything that says "lol" and replace it with the id value of the first previous chapter tag? maybe something like this.</p>

<p>python</p>

<pre><code>x=open('source.txt')
lines = x.readlines()
for line in lines:
  if line.startswith('&lt;text'):
    line.replace('lol', first previous chapter id value)
x.write(lines)
x.close()
</code></pre>

<p>source text</p>

<pre><code>&lt;chapter id="1"&gt;
&lt;text class="lol"&gt;
&lt;text class="lol"&gt;
&lt;chapter id="2"&gt;
&lt;text class="lol"&gt;
&lt;text class="lol"&gt;
&lt;chapter id="3"&gt;
&lt;text class="lol"&gt;
&lt;text class="lol"&gt;
&lt;chapter id="4"&gt;
&lt;text class="lol"&gt;
&lt;text class="lol"&gt;
</code></pre>

<p>result text</p>

<pre><code>&lt;chapter id="1"&gt;
&lt;text class="1"&gt;
&lt;text class="1"&gt;
&lt;chapter id="2"&gt;
&lt;text class="2"&gt;
&lt;text class="2"&gt;
&lt;chapter id="3"&gt;
&lt;text class="3"&gt;
&lt;text class="3"&gt;
&lt;chapter id="4"&gt;
&lt;text class="4"&gt;
&lt;text class="4"&gt;
</code></pre>
