6

There are other questions referring to this on Stack Overflow such as how-to-parse-a-string-and-return-a-nested-array?

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?

1 Answer 1

4

Based on this solution by falsetru:

import re

def parse_nested(text, left=r'[(]', right=r'[)]', sep=r','):
    """ Based on https://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']]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.