Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python parsing bracketed blocks

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 an input should return:

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

which should return:

[ "{ c }" ]  [ "c" ]  [] 
like image 440
Martin Avatar asked Oct 30 '09 18:10

Martin


1 Answers

Or this pyparsing version:

>>> from pyparsing import nestedExpr >>> txt = "{ { a } { b } { { { c } } } }" >>> >>> nestedExpr('{','}').parseString(txt).asList() [[['a'], ['b'], [[['c']]]]] >>> 
like image 50
PaulMcG Avatar answered Sep 22 '22 23:09

PaulMcG