my $string = '/whatever/filepath/and/name.dat'; my $filename; if ( $string =~ m#/([^/]+)$# ) { $filename = $1; print $filename, "\n"; }