in reply to Re: remove repeating characters from strings
in thread remove repeating characters from strings

The problem with that approach is that from the string ccabcabcab, it will group the first cc and only the second and third cab, while the question indicated the longest match should be favourited, not the left most.

Here's a solution that favours the longest match, finds recursive repeatitions, and uses {$n} instead of *.

$_ = "bbbccabcabcabcdba"; my $l = length; s{((\w{$l})\2+)}{($2){@{[length($1)/length($2)]}}}g while -- $l;
This will produce:
(b){3}c(cab){3}cdba
Changing $_ to bbbccaabcaabcaabcdba, the above code will yield:
(b){3}c(c(a){2}b){3}cdba

Abigail