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>I would use this scheme, given <code>t</code> being your text:</p>

<pre><code>last = re.findall('\([^())]+\)', t)[-1]
</code></pre>

<p>The regex searches for an opening parenthesis, then take everything that is neither opening nor closing parenthesis, and then matches the closing parenthesis. Since there could be more than one like this, I use <code>findall</code> and take the last one.</p>

python regex