The easiest way to do this is in a loop:
#!/usr/bin/perl my $string = "this aaa and bbbb for ### ## ppppppp"; my $max = ""; while ($string =~ /((.)\2+)/gs) { $max = $1 if length($1) > length($max); } print "$max\n";
You can also use reduce , but this is less efficient:
#!/usr/bin/perl use List::Util "reduce"; my $string = "this aaa and bbbb for ### ## ppppppp"; my $max = reduce { length($b) > length($a) ? $b : $a } "", $string =~ /((.)\2+)/gs; print "$max\n";
If you want to get only one task, simply:
#!/usr/bin/perl my $string = "this aaa and bbbb for ### ## ppppppp"; my $max = ( sort { length($b) <=> length($a) } "", $string =~ /((.)\2+)/g)[0]; print "$max\n";
All three answers create ppppppp for this example line.
They also return an empty string if there is no such sequence, and they return the first such sequence in case of a link.
tchrist
source share