Related to : Python parsing bracketed blocks
I have a file with the following format :
#
here
are
some
strings
#
and
some
others
#
with
different
levels
#
of
#
indentation
#
#
#
So a block is defined by a starting #
, and a trailing #
. However, the trailing #
of the n-1th block is also the starting #
of the nth block.
I am trying to write a function that, given this format, would retrieve the content of each blocks, and that could also be recursive.
To start with, I started with regexes but I abandonned quite fast (I think you guessed why), so I tried using pyparsing
, yet I can't simply write
print(nestedExpr('#','#').parseString(my_string).asList())
Because it raises a ValueError
Exception (ValueError: opening and closing strings cannot be the same
).
Knowing that I cannot change the input format, do I have any better option than pyparsing
for this one ?
I also tried using this answer : https://stackoverflow.com/a/1652856/740316, and replaced the {
/}
with #/#
yet it fails to parse the string.