use strict; use warnings; my %searches = ( bar => " bar \n\n", bar1 => " bar1 \nw\n", baz => "baz", foo => " foo c \n", foo1 => " foo ", ); my $x; my $regex = qr{ ^ \s* (??{ $x }) \s* $ }x; for (sort keys %searches) { $x = $_; print "$_: ", $searches{$_} =~ $regex ? 'match' : 'no match', "\n"; }