DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Return The Extension From A File Name

09.06.2006
| 15412 views |
  • submit to reddit
        // Return the extension from a file name

/(.*\.)(.*$)/.match(File.basename(file_name))[2]
    

Comments

Snippets Manager replied on Fri, 2009/02/27 - 3:07pm

file_ending = filename[/(?:.*\.)(.*$)/, 1] Dont capture everything in the first brackets .. everything before the last dot . and return the first(second in ary) component of the MatchData. Play with it at rubular: http://rubular.com/regexes/6334

Snippets Manager replied on Mon, 2012/05/07 - 1:27pm

File.extname(file_name)