You are going to be given a string containing some alphabetic characters along with [
and ]
. Your task is to split into sections that are enclosed in a "group" created by [...]
and those that are not. e.g.
"absbn[mesl]meslo[eyyis]me"
->
"absbn"
"mesl"
"meslo"
"eyyis"
"me"
Super simple right? Well hold your horses. What happens when multiple brackets conflict? like co[[x]t
or m[[e]]it
? We don't have any guarantee that the brackets are matched or not nested.
The rule we are going to use is that when multiple possible groups could be made we will always choose the smallest ones. Any [
s left without a match are just regular characters and appear in the output.
So in co[[x]t
we could do co
[x
t
, but we could also do co[
x
t
. Since the group here is either [x
or x
we choose the smaller one and the result is co[
x
t
. In the second case me[[e]]it
there are 4 ways to make groups here, but unambiguously me[
e
]it
results in the smallest group.
Your task will be to take a non-empty string of characters a
through z
plus two brackets of your choice ([]
, {}
, ()
and <>
) and to split it as described above, providing a list of strings as output.
In cases where a group is on the boundary of the string, e.g. aaa[b]
or [ems]ee
you may choose to include or omit an empty string ""
on that boundary. The test cases always include them.
This is code-golf. Answers will be scored in bytes with the goal being to minimize the size of your source code.
Test cases
go[cat]me -> "go" "cat" "me"
absbn[mesl]meslo[eyyis]me -> "absbn" "mesl" "meslo" "eyyis" "me"
co[[x]t -> "co[" "x" "t"
me[[e]]t -> "me[" "e" "]t"
mon[g]]u -> "mon" "g" "]u"
msy]mesl -> "msy]mesl"
eoa[m -> "eoa[m"
a[b[ -> "a[b["
mesi]mmp[mo -> "mesi]mmp[mo"
meu[ems[mela[] -> "meu[ems[mela" "" ""
w[[[] -> "w[[" "" ""
[foo]foobar[bar] -> "" "foo" "foobar" "bar" ""
[][] -> "" "" "" "" ""