use warnings; use strict; my $str = 'xpy7pG'; my $char = 'p'; if ($str =~ /^\w{4}$char\w$/){ print "matched\n"; }