Your challenge is to expand some brackets in a program's input as shown:
- Find a string s between two matching brackets
], with a single digit n after the closing bracket.
- Remove the brackets.
- Replace s with itself repeated n times. (If n is 0, simply remove s.)
- Go to step 1 until there are no more matching brackets in the input.
Additional rules and clarifications:
- You will take input and give output via any allowed means.
- A trailing newline in the output is allowed.
- You only need to handle printable ASCII in the input.
- You may assume that all brackets match, i.e. you will never receive the input
- You may assume that each closing bracket
]has a digit after it.
Input -> Output [Foo[Bar]3]2 -> FooBarBarBarFooBarBarBar [one]1[two]2[three]3 -> onetwotwothreethreethree [three[two[one]1]2]3 -> threetwoonetwoonethreetwoonetwoonethreetwoonetwoone [!@#[$%^[&*(]2]2]2 -> !@#$%^&*(&*($%^&*(&*(!@#$%^&*(&*($%^&*(&*( [[foo bar baz]1]1 -> foo bar baz [only once]12 -> only once2 [only twice]23456789 -> only twiceonly twice3456789 [remove me!]0 -> before [in ]2after -> before in in after
As this is code-golf, the shortest answer in each language wins. Good luck!