So I have a string like this:
A!
B!
C!
<tag>
D!
E!
</tag>
F!
<tag>
G!
</tag>
Is it possible to parse this with a regex so I get this output (a list):
[A, B, C, [D, E], F, [G]]
Basically I’m looking for a way to split the string by ! and by the tag…and the tag part can happen anywhere…and multiple times (but not recursively…meaning a tag within a tag…this doesn’t happen). The whole thing seems regular…is this even possible to do with regex?
EDIT: I am using Python
EDIT2: I am only using A, B, C… as a representation…those can be any string made out of letters and numbers
I use a deque for speed because pop(0) would be very slow, and reversing the list and using pop() would make the function harder to read and understand.
I dare anyone to create a regexp doing the same, while also improving clarity!
(BTW, I think you could also use the pyparsing module to solve this problem, since it supports recursion.)
EDIT: Changed function to expect either string or deque as argument, simplifying invocation.