Regular expression for separating strings
<p>I have the following string</p>

<blockquote>
  <p>Long string 1 [sucess 50] long string 2 [apple 5 banana 20 orange 75]
  long string 3 [failure: 100]</p>
</blockquote>

<p>Now I want to split it into three strings:</p>

<ol>
<li>Long string 1 [sucess 50]<br/></li>
<li>long string 2 [apple 5 banana 20 orange 75]<br/></li>
<li>long string 3 [failure: 100]<br/></li>
</ol>

<p>inside [] there could be one or more instances of combination of a string and a number, and the number is from 0 to 100.</p>

<p>How this can be done with regex in python?</p>

<p>No need for regular expressions in such a simple task. Just use Python's built-in <code>str.replace()</code> method like so:</p>

<pre><code>your_str = your_str.replace(']', ']\n')
</code></pre>

<p>To split it and get a list, use <code>your_str.split(']')</code></p>

<p>But if you really want regex or the string inside the brackets can contain more brackets, you can try this:</p>

<pre><code>import re

your_str = "Long string 1 [sucess 50] long string 2 [apple 5 banana 20 orange 75] long string 3 [failure: 100]"

your_str = re.sub(r'\[(.+? [0-9]+)\] ', r'[\1]\n', your_str)

print(your_str)
</code></pre>

<p>The following code produces this output:</p>

<pre><code>Long string 1 [sucess 50]
long string 2 [apple 5 banana 20 orange 75]
long string 3 [failure: 100]
</code></pre>

<p>But this is going to be a string. To get a list of strings, you can then simply split by newline like this:</p>

<pre><code>your_str.split('\n')
</code></pre>

python regex