Detecting loops in in in strings
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);