How to split a string to get the last item in braces?
<p>I'm trying to split a string to extract the last item in braces.</p>

<p>For example if I had the string</p>

<pre><code>'Stud Rd/(after) Ferntree Gully Rd (Scoresby)'
</code></pre>

<p>I would like to split it into the components</p>

<pre><code>('Stud Rd/(after) Ferntree Gully Rd', 'Scoresby')
</code></pre>

<p>So far I've written the following regex to do this</p>

<pre><code>re.search(r'^(.*) \((.*)\)$', string)
</code></pre>

<p>However this breaks in the case of an input like</p>

<pre><code>'Bell St/Oriel Rd (Bellfield (3081))'
</code></pre>

<p>Which I would like be split into</p>

<pre><code>('Bell St/Oriel Rd', 'Bellfield (3081)')
</code></pre>

<p>Is there a better way then this to approach this problem?</p>

<p>You can use this lazy regex:</p>

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

<p><a href="https://regex101.com/r/yQ8xI8/1" rel="nofollow">RegEx Demo</a></p>

<p>Examples:</p>

<pre><code>&gt;&gt;&gt; reg = r'(.*?) \((.*)\)[^()]*$'

&gt;&gt;&gt; s = 'Bell St/Oriel Rd (Bellfield (3081))'
&gt;&gt;&gt; re.findall(reg, s)
[('Bell St/Oriel Rd', 'Bellfield (3081)')]

&gt;&gt;&gt; s = 'Stud Rd/(after) Ferntree Gully Rd (Scoresby)'
&gt;&gt;&gt; re.findall(reg, s)
[('Stud Rd/(after) Ferntree Gully Rd', 'Scoresby')]
</code></pre>

python regex