use warnings; use strict; my $str = 'xpy7pG'; my $char = 'p'; if ($str =~ /^\w{4}$char\w$/){ print "matched\n"; } #### /^[0-9A-Za-z]{4}$char[0-9A-Za-z]$/