Extract last string between brackets from a string - python
Question: I would like to know the way for extracting the last string between brackets
from a long string. So I need a function, `extract_last`, for example, that
let me get an output like that:

    
    
    >> extract_last('(hello) my (name) is (Luis)')
    >> 'Luis'
    

How can I accomplish that without using a for, I am looking for the smartest
way.

The for way that I have implemented works. I have no tested it with all the
possibilities, but the simplest things do it well:

    
    
    def extract_last(string):
        bracket_found = False
        characters = []
        for character in string[::-1]:
            if character == ')':
                bracket_found = True
                continue
            if(character == '(' and bracket_found):
                break;
           if(bracket_found and character != ')'):
                characters.append(character)
        return ''.join(characters[::-1])
    

But this solution has many lines and I know that using regular expressions or
something like that, I could accomplish that with a one-or-two-lines solution.


Answer: 
    import re
    
    def extract_last(val):
        r = re.findall(r'\((\w+)\)', val)
        return r[-1] if r else None
    

