in reply to optimise perl code

As mentioned above, File::Basename is probably best, but if you want a RE the node Removing underscores. discussed RE's of the same nature.