# custom word boundary: my $wb = qr{ ^ | $ | \s }x; # you have to take care if you are checking for a left or a right word boundary: if ($str =~ m/(?<=$wb)$termstr1(?=$wb)/){ ... }