pythonsubstringstring-parsings-expressionlongest-substring

Python - Longest substring having only matched parentheses


For interactive parsing purposes, given an input string I need to extract the longest possible substring starting at index 0 and having only matched parentheses.

Example (LISP-like s-expressions)

Input string: (print "hello") (assign a (+ c d)) (assign e (+ f g)

Output substring: (print "hello") (assign a (+ c d))

I would like to make a simple Python function to achieve this.


Solution

  • Loop over the string while counting parentheses, and at the end just slice the string to the last index where the parenthesis counter was 0:

    def max_parseable_substring(text):
        parentheses = 0
        end = 0
    
        for i, char in enumerate(text):
            if char == "(":
                parentheses += 1
            elif char == ")":
                parentheses -= 1
    
            if parentheses == 0:
                end = i
    
        return text[:end]