Which is the most efficent of matching and replacing with an identifier every three new lines?
<p>I am working with some .txt files that doesn't have structure (they are messy), they represent a number of pages. In order to give them some structure I would like to identify the number of pages since the file itself doesn't have them. This can be done by replacing every three newlines with some annotation like: </p>

<pre><code>\n
    page: N
\n
</code></pre>

<p>Where <code>N</code> is the number. This is how my files look like, and I also tried with a simple <a href="http://pastebin.com/K0PdJ1TG" rel="nofollow"><code>replace</code></a>. However, this function confuses and does not give me the expected format which would be something like <a href="http://pastebin.com/ZCw63uyE" rel="nofollow">this</a>. Any idea of how to replace the spaces with some kind of identifier, just to try to parse them and getting the position of some information (page)?.</p>

<p>I also tried this:</p>

<p>import re</p>

<pre><code>replaced = re.sub('\b(\s+\t+)\b', '\n\n\n', text)
print (replaced)
</code></pre>

<p>If the format is as regular as you state in your problem description:</p>

<blockquote>
  <p>Replace every occurrence of three newlines <code>\n</code> with <code>page: N</code></p>
</blockquote>

<p>You wouldn't have to use the <code>re</code> module. Something as simple as the following would do the trick:</p>

<pre><code>&gt;&gt;&gt; s='aaaaaaaaaaaaaaaaa\n\n\nbbbbbbbbbbbbbbbbbbbbbbb\n\n\nccccccccccccccccccccccc'
&gt;&gt;&gt; pages = s.split('\n\n\n')
&gt;&gt;&gt; ''.join(page + '\n\tpage: {}\n'.format(i + 1) for i, page in enumerate(pages))
'aaaaaaaaaaaaaaaaa\n\tpage: 1\nbbbbbbbbbbbbbbbbbbbbbbb\n\tpage: 2\nccccccccccccccccccccccc\n\tpage: 3\n'
</code></pre>

<p>I suspect, though, that your format is less regular than that, but you'll have to include more details before I can give a good answer for that.</p>

<p>If you want to split with messy whitespace (which I'll define as <em>at least</em> three newlines with any other whitespace mixed in),  you can replace <code>s.split('\n\n\n')</code> with:</p>

<pre><code>re.split(r'(?:\n\s*?){3,}', s)
</code></pre>

python regex python-3.x nlp text-processing