use strict; use warnings; use v5.12; my $string = "abcdefggggggggggg"; my $position = length( $string ) - 1; my $find = substr $string, $position, 1; $position-- while substr( $string, $position, 1 ) eq $find; substr( $string, $position + 3, length( $string ) - ( $position + 3 ), '') if length( $string ) - $position > 3; say $string;