Turn a string with nested parenthesis into a neste

2019-07-03 21:54发布

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条回答
对你真心纯属浪费
2楼-- · 2019-07-03 22:28

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']]]
查看更多
登录 后发表回答