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>You don't necessarily need regular expressions and can approach it line by line checking if a line starts with <code>Block</code> collecting the results into a dictionary:</p>

<pre><code>from collections import defaultdict

data = defaultdict(list)
with open("input.txt") as f:
    for line in f:
        if line.startswith("Block"):
            key = line.strip()
        else:
            data[key].append(line.strip())

print(dict(data))
</code></pre>

<p>Prints:</p>

<pre><code>{
  'Block3': ['a 1.01 2.02', '0.01 2.11'], 
  'Block2': ['v 0.00 2.01', '0.01 2.11'], 
  'Block1': ['u 0.00 2.00', '0.11 2.11']
}
</code></pre>

python regex