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>Always, <em>ALWAYS</em> use raw strings when working with regular expressions in Python. <code>\b</code> means backslash inside a string, it gets evaluated and your regex gets damaged. Just add an 'r' in front of string.
This will do the trick:</p>

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

python regex