It's simpler than that, just use regex greediness. You were very close.
my $re = qr/((.)\2+)/;
Here's an example,
$ perl -e'my $re = qr/((.)\2+)/; $_="aabccccdddeffff"; while (m/$re/g)
+ { printf "\"%s\" x %d\n", $2, length($1) }'
"a" x 2
"c" x 4
"d" x 3
"f" x 4
$
That skips capturing lone characters as a sequence of one - change the '+' quantifier to '*' to get them, too. There is no practical limit on the length of the match.
I didn't address picking out the maximum length substring captured. There are lots of ways to do that.
Update: Ok, here's an easy way to get the max length as the search is done, using the (?{}) construct.
use re 'eval';
my $re = qr/((.)\2+)/;
my ($maxlen, $maxchr, $maxloc);
$_="aabccccdddeffff";
1 while m/$re(?{
$maxlen = length($^N),
$maxchr = substr($^N,0,1),
$maxloc = pos() - $maxlen
if length($^N) > $maxlen;
})/g;
print $maxchr, ' x ', $maxlen, ' at ', $maxloc, $/;
__END__
c x 4 at 3
Access to the original matching chunk of the string is given by substr($_, $maxloc, $maxlen).
|