use strict; use warnings; my $string = 'ABCDEFGHIJKLMNOPQ'; my $tail = 'OPQ'; my $preLen = 4; if ($string =~ /(.{$preLen})$tail/) { print "$1\n"; }