$str =~ s/_(\.mp3)$/$1/g; # Or Much faster with the same result: substr($str, -5, 1, "") if(substr($str, -5, 1) eq '_');