I have this string:
>>> s = 'Evolution Recruitment (Agency) (London, UK)'
I want to get this part:
London, UK
Keep in mind that for the real case I'm working on the first brackets (agency)
are not necessarily in the string.
I tried this:
>>> import re
>>> re.findall("\((.*?)\)$", s)
['Agency) (London, UK']
If I was able to let the regex read from right to left instead of left to right this solution should work.
Is that possible? If not, is there another way to get the part London, UK
?
In [8]: re.search(r".*[(](.*)[)]", s).groups()
Out[8]: ('London, UK',)
It just uses a greedy .*
match to get to the last set of parentheses.
Alternatively, you could find all matching parentheses, and just use the last pair:
In [14]: re.findall(r'\(.*?\)', s)[-1]
Out[14]: '(London, UK)'
The regex approach is quite flexible. However, if you know the input's well-formed and you just want the text inside the last set of parentheses:
In [11]: s[s.rfind('(')+1:s.rfind(')')]
Out[11]: 'London, UK'
This scans the string right-to-left, so could potentially be fairly efficient (I have profiled anything, so that's just a speculation).