my $str = "The quick abc"; for my $regex ('\babc\z', '\babc\Z') { print "Matched with newline using $regex\n" if "$str\n" =~ $regex; print "Matched without newline using $regex\n" if $str =~ $regex; } #### Matched without newline using \babc\z Matched with newline using \babc\Z Matched without newline using \babc\Z