Simon Gibbons
Simon Gibbons

Reputation: 7194

How to split a string to get the last item in braces?

I'm trying to split a string to extract the last item in braces.

For example if I had the string

'Stud Rd/(after) Ferntree Gully Rd (Scoresby)'

I would like to split it into the components

('Stud Rd/(after) Ferntree Gully Rd', 'Scoresby')

So far I've written the following regex to do this

re.search(r'^(.*) \((.*)\)$', string)

However this breaks in the case of an input like

'Bell St/Oriel Rd (Bellfield (3081))'

Which I would like be split into

('Bell St/Oriel Rd', 'Bellfield (3081)')

Is there a better way then this to approach this problem?

Upvotes: 0

Views: 348

Answers (4)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Change your regex pattern and work with match object(returned by search function) in proper way:

import re

str = 'Bell St/Oriel Rd (Bellfield (3081))'
result = re.search(r'^(.*?) \((.*?)\)$', str)

print(result.group(1,2))   # ('Bell St/Oriel Rd', 'Bellfield (3081)')

Upvotes: 1

anubhava
anubhava

Reputation: 785406

You can use this lazy regex:

(.*?) \((.*)\)[^()]*$

RegEx Demo

Examples:

>>> reg = r'(.*?) \((.*)\)[^()]*$'

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

>>> s = 'Stud Rd/(after) Ferntree Gully Rd (Scoresby)'
>>> re.findall(reg, s)
[('Stud Rd/(after) Ferntree Gully Rd', 'Scoresby')]

Upvotes: 4

bravosierra99
bravosierra99

Reputation: 1371

This works assuming you don't have any parenthesis prior to the last chunk.

var = 'Bell St/Oriel Rd', 'Bellfield (3081)'.split('(')
var[-1] = var[-1][:-1]

Upvotes: 0

Israel Unterman
Israel Unterman

Reputation: 13510

I would use this scheme, given t being your text:

last = re.findall('\([^())]+\)', t)[-1]

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 findall and take the last one.

Upvotes: 0

Related Questions