Regular expressions not splitten Python
<p>Using python I'm trying to divide a text file in blocks using regular expression. The text file looks like this:</p>

<pre><code>Block1
  u 0.00 2.00
  0.11 2.11
Block2
  v 0.00 2.01
  0.01 2.11
Block3
  a 1.01 2.02
  0.01 2.11
</code></pre>

<p>my regular expression</p>

<pre><code>re.split("(\bBlock1\b\n\s\s[u].*\n.*)", open('Blockfile.txt', "r").read())
</code></pre>

<p>However when I run the code it doesn't split.
see my regex code here: <a href="https://regex101.com/r/jW7oP4/2" rel="nofollow">https://regex101.com/r/jW7oP4/2</a></p>

<p>Thanks!!</p>

<p><code>Split</code> only splits on the argument with its speech marks, for example:</p>

<p>Splitting <em>"this string"</em> with <code>.split(" ")</code> results in:</p>

<pre><code>["this","string"] 
</code></pre>

<p>But splitting it with <code>.split("s ")</code> results in:</p>

<pre><code>["thi", "string"]
</code></pre>

<p>Rather than:</p>

<pre><code>["thi", "tring"]
</code></pre>

<p>Which is your problem. Your code will only split when it gets <code>(\bBlock1\b\n\s\s[u].*\n.*)</code> all in one go!</p>

<p>I suggest using multiple split functions or a different function like <code>translate</code>. </p>

python regex