my $last= $string =~ /.*(PATTERN)/s ? $1 : undef; # or my( $last )= $string =~ /.*(PATTERN)/sg;