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>

<pre><code>In [79]: import re

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

In [81]: re.findall(r'([^\[]*\[[^\]]*\])(?: ?)', my_string)
Out[81]: 
['Long string 1 [sucess 50]',
 'long string 2 [apple 5 banana 20 orange 75]',
 'long string 3 [failure: 100]']
</code></pre>

<p>How the regex works:</p>

<pre><code>([^\[]*\[[^\]]*\])(?: ?)
</code></pre>

<p><img src="https://www.debuggex.com/i/pH92LSnQn9168BW5.png" alt="Regular expression visualization"></p>

<p><a href="https://www.debuggex.com/r/pH92LSnQn9168BW5" rel="nofollow">Debuggex Demo</a></p>

<p>Detailed explanation at: <a href="https://regex101.com/r/oS1oL8/1" rel="nofollow">https://regex101.com/r/oS1oL8/1</a></p>

python regex