parsing nested parentheses in python, grab content by level

后端 未结 3 1878
日久生厌
日久生厌 2020-11-29 07:46

Apparently this problem comes up fairly often, after reading

Regular expression to detect semi-colon terminated C++ for & while loops

and thinking about

3条回答
  •  渐次进展
    2020-11-29 08:25

    Parentheses matching requires a parser with a push-down automaton. Some libraries exist, but the rules are simple enough that we can write it from scratch:

    def push(obj, l, depth):
        while depth:
            l = l[-1]
            depth -= 1
    
        l.append(obj)
    
    def parse_parentheses(s):
        groups = []
        depth = 0
    
        try:
            for char in s:
                if char == '(':
                    push([], groups, depth)
                    depth += 1
                elif char == ')':
                    depth -= 1
                else:
                    push(char, groups, depth)
        except IndexError:
            raise ValueError('Parentheses mismatch')
    
        if depth > 0:
            raise ValueError('Parentheses mismatch')
        else:
            return groups
    
    print(parse_parentheses('a(b(cd)f)')) # ['a', ['b', ['c', 'd'], 'f']]
    

提交回复
热议问题