my $localfilename; if($filename =~ m/(.*?$CGI::SL)*(.*)(?=$)/ ) { $localfilename = $2; } else { # handle the error however you need to }