Remove all nested blocks, whilst leaving non-nested blocks alone via python
Source: [This] is some text with [some [blocks that are nested [in a [variety] of ways]]] Resultant text: [This] is some text with I don't think you can do a regex for this, from looking at the threads at stack overflow . Is there a simple way to to do this -> or must one reach for pyparsing (or other parsing library)? Taking the OP's example as normative (any block including further nested blocks must be removed), what about...: import itertools x = '''[This] is some text with [some [blocks that are nested [in a [variety] of ways]]] and some [which are not], and [any [with nesting] must go]