python - Extract string inside nested brackets -
i need extract strings nested brackets so:
[ [ hello [ ] [what ] other side ] slim shady ]
result (order doesn't matter):
this slim shady hello other side
note, string have n brackets, , valid, may or may not nested. also, string doesn't have start bracket.
the solutions have found online similar problem suggest regex, i'm not sure work in case.
i thinking of implementing similar how check if string has valid parentheses:
walk through string. if see [ push index on stack, if see ], substring there current spot.
however, we'd need erase substring original string don't part of of outputs. so, instead of pushing pushing index stack, thinking of creating linkedlist go along, , when find [ insert node on linkedlist. allow delete substring linkedlist.
would approach or there cleaner, known solution?
edit:
'[ [ hello [ ] [what ] other [side] ] slim shady ][oh [g[a[w[d]]]]]'
should return (order doesn't matter):
this slim shady hello other side oh g w d
white spaces don't matter, that's trivial remove afterwards. matters being able distinguish different contents within brackets. either separating them in new lines, or having list of strings.
this code scans text character , pushes empty list
on stack every opening [
, pops last pushed list
stack every closing ]
.
text = '[ [ hello [ ] [what ] other side ] slim shady ]' def parse(text): stack = [] char in text: if char == '[': #stack push stack.append([]) elif char == ']': yield ''.join(stack.pop()) else: #stack peek stack[-1].append(char) print(tuple(parse(text)))
output;
(' ', 'what ', ' hello other side ', ' slim shady ') (' ', 'what ', 'side', ' hello other ', ' slim shady ', 'd', 'w', 'a', 'g', 'oh ')
Comments
Post a Comment