Perl 5+-nF -M5.10.0
, 93 bytes
Thanks to @Fmbalbuena for noticing I'd missed printing the unaltered string! Thanks to @emanresu A and @Neil's regex wizardry saving 14 (!!) bytes!
@@=<>;say$"x4,$_,map$"x(4+$-).$_.$"x(@F-($-+=y///c)).$@[index'><+-.,[]',$_],/[^]<>+-.[]+|./g
Explanation
First print
4
x
(string repetition operator) $"
(which defaults to space), then store the length of the input (implicitly obtained via -F
into @F
) in $=
, and store the explanations in @@
. Then, for
each matching piece of code (either one char from -<>+.,[]
, or any number of any other chars) concatenate the following with $\
: 4 +$-
(which starts out as 0
) x
$"
, followed by the piece of code ($_
), followed by the original length, minus $-
(to which we add the current length of this code piece) x
$"
and finally, using the index
function on the string ><+-.,[]
, the message associated to the current piece of code (index
returns -1
for missing index, for which Perl returns the last element of the list). $_
is output followed by $\
thanks to -p
flag.
Perl 5+-F/([^]<>+-.[]+|.)/ -M5.10.0
, 92 bytes
The regex used in the above, can be provided via -F
to save a byte.
$==y///c;@@=<>;say$"x4,$_,map/./&&$"x(4+$-).$_.$"x($=-($-+=y///c)).$@[index'><+-.,[]',$_],@F