# Function to find extension in file name string

**URL:** <https://www.ruby-forum.com/t/function-to-find-extension-in-file-name-string/125342>\
**Category:** Rails\
**Created:** [January 8, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/function-to-find-extension-in-file-name-string/125342 "2008-01-08T13:07:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@bartee](https://www.ruby-forum.com/u/bartee)\
**Post date:** [January 8, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/function-to-find-extension-in-file-name-string/125342/1 "2008-01-08T13:07:46Z")

</div>

I am very new to regular expressions. ( and RoR ).

I need to find the extension in a filename on a string.

I tried several RE’s but cannot get one to work that will find the 1st  
dot from the end of a file name.

Also, to use this as a function for all of the applications I think it  
should go in the LIB subdirectory. Is that correct?

TIA

---

<div class="post-metadata">

**Author:** ![bartee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@bartee](https://www.ruby-forum.com/u/bartee)\
**Post date:** [January 8, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/function-to-find-extension-in-file-name-string/125342/2 "2008-01-08T13:12:42Z")

</div>

On Jan 8, 2008, at 1:07 PM, Bartee L. wrote:

> I am very new to regular expressions. ( and RoR ).
> 
> I need to find the extension in a filename on a string.

Please check File.extname.

– fxn

---

<div class="post-metadata">

**Author:** ![bartee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d78d45/32.png) [@bartee](https://www.ruby-forum.com/u/bartee)\
**Post date:** [January 8, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/function-to-find-extension-in-file-name-string/125342/3 "2008-01-08T14:02:33Z")

</div>

Xavier N. wrote:

> On Jan 8, 2008, at 1:07 PM, Bartee L. wrote:
> 
> > I am very new to regular expressions. ( and RoR ).
> > 
> > I need to find the extension in a filename on a string.
> 
> Please check File.extname.
> 
> – fxn

Welllllllllll…

LOL… Really…

I hate when the answer is soooo simple.

I guess I need to start reading ALL of the specs.

But Thanks for your quick response and EXCELLENT answer.

…bartee…
