tags:

views:

37

answers:

3

I have

  file_ext = attach.document_file_name.capture(/\.[^.]*$/)

but i guess there is no method capture.

I'm trying to get the file extension from a string. I don't yet have the file.

+1  A: 

You can do RegEx match in ruby like so:

file_ext = (/\.[^.]*$/.match(attach.document_file_name.to_s)).to_s

Fore more information please check http://ruby-doc.org/core/classes/Regexp.html

GeekTantra
+3  A: 

How about:

file_ext = attach.document_file_name[/\.[^.]*$/]
giraff
This also should work. Nice trick. +1 from me.
GeekTantra
Note that it returns `nil` if no extension is found. If you prefer an empty string, append a to_s at the end of line.
giraff
+5  A: 

There is also the built-in ruby function File.extname:

file_ext = File.extname attach.document_file_name

(with the difference that File.extname('hello.') returns '', whereas your regex would return '.')

giraff
+1 Why mess with regular expressions when there is a function that does exactly what you want
bta