Detecting loops in in in strings Given a string, mark which parts in the string repeat in box brackets, and write how many times it does so before the box.

Example:st st st Hello ThereThere
st(with a space after) repeats 3 times, There repeats 2 times, and l repeats 2 times in Hello, so the output will be:
3[st ]He2[l]o 2[There]
There is no space between [st ] and Hello because the space in the string is being taken up by the repeating part. [st ], 3 times with a Hello after it would be st st st Hello, which is what the string has.

Other examples:
st Hello st st Bye -> st He2[l]o 2[st ]Bye
Bookkeeper -> B2[o]2[k]2[e]per
/\/\/\\\\\\\\/\/\/ -> 3[/\]6[\]3[\/]

Shortestest code wins.

EDIT: To clarify the choice of selections, choose the largest possible repeating section, and try and leave as little non-repeating characters as possible. Do not nest boxed-in sections.
Perl, 36 bytes

#!perl -p
s!(.+)\1+!@{[$&amp;=~/\Q$1/g]}."[$1]"!eg


Try it online!



PHP 5.6, 68 bytes

Same code in PHP. Requires the command line option -F, and relies on deprecated /e modifier.



&lt;?=preg_filter('/(.+)\1+/e','substr_count("\0","\1")."[\1]"',$argn);