Turn a string with nested parenthesis into a nested list, python
Question: There are other questions referring to this on Stack Overflow such as [how-to-
parse-a-string-and-return-a-nested-
array?](http://stackoverflow.com/questions/17140850/how-to-parse-a-string-and-
return-a-nested-array?lq=1)

But they all refer to lists in the format of `((abc)de(fg)))`. going to the
form:`[['a','b','c']'d','e'['f','g',]]]` I have a list of the form:

    
    
    ((wordOneWord2)OtherWord(FinalWord)))
    

By using the methods I learnt from the other questions by nested list was of
the form:

    
    
    [['w','o','r','d','O','n','e','W','o','r','d','2']'O','t','h','e','r','W','o','r','d',['F','i','n','a','l','W','o','r','d']]]
    

rather than the desired

    
    
    [['wordOneWord2'], 'OtherWord', ['FinalWord']]
    

I can achieve the desired result by parsing the list letter by letter and then
concatenating the items within each list back together but it takes more code
than I think necessary, is there a faster way of doing this?


Answer: Based on this [solution by
falsetru](http://stackoverflow.com/a/17141899/190597):

    
    
    import re
    
    def parse_nested(text, left=r'[(]', right=r'[)]', sep=r','):
        """ Based on http://stackoverflow.com/a/17141899/190597 (falsetru) """
        pat = r'({}|{}|{})'.format(left, right, sep)
        tokens = re.split(pat, text)    
        stack = [[]]
        for x in tokens:
            if not x or re.match(sep, x): continue
            if re.match(left, x):
                stack[-1].append([])
                stack.append(stack[-1][-1])
            elif re.match(right, x):
                stack.pop()
                if not stack:
                    raise ValueError('error: opening bracket is missing')
            else:
                stack[-1].append(x)
        if len(stack) > 1:
            print(stack)
            raise ValueError('error: closing bracket is missing')
        return stack.pop()
    
    text = '((wordOneWord2)OtherWord(FinalWord))'
    print(parse_nested(text))
    # [[['wordOneWord2'], 'OtherWord', ['FinalWord']]]
    

