Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

What would be the best way in Python to parse out chunks of text contained in matching brackets?

"{ { a } { b } { { { c } } } }"

should initially return:

[ "{ a } { b } { { { c } } }" ]

putting that as input should return:

[ "a", "b", "{ { c } }" ]

Which should return:

[ "{ c }" ]

[ "c" ]

[]

1 Answer

0 votes
by (16.8k points)

You can use the Psuedocode like this:

For each string in the array:

    Find the first '{'. If there is none, leave that string alone.

    Init a counter to 0. 

    For each character in the string:  

        If you see a '{', increment the counter.

        If you see a '}', decrement the counter.

        If the counter reaches 0, break.

    Here, if your counter is not 0, you have invalid input (unbalanced brackets)

    If it is, then take the string from the first '{' up to the '}' that put the

     counter at 0, and that is a new element in your array.

Or else, use a parsing version:

>>> from pyparsing import nestedExpr

>>> txt = "{ { a } { b } { { { c } } } }"

>>>

>>> nestedExpr('{','}').parseString(txt).asList()

[[['a'], ['b'], [[['c']]]]]

>>>

Related questions

Browse Categories

...