# Working with String

**URL:** <https://www.ruby-forum.com/t/working-with-string/112402>\
**Category:** Ruby\
**Created:** [August 18, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/working-with-string/112402 "2007-08-18T14:03:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [August 18, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/working-with-string/112402/1 "2007-08-18T14:03:21Z")

</div>

Hi need some help with string manipulation. I have a string representing  
full path of a file. Example “/foo/foo.gif”. I need to just extract the  
file name i.e “foo.gif” from this string. I also need to extract the  
file type i.e “gif” from it in a separate string. Can anybody tell me  
how to do it using regular expressions in Ruby. I know this can be done  
using String.gsub, but i am too bad at reg expressions. And if someone  
can point me to good source for learning reg expressions then it would  
be really helpful.

Thanks

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [August 18, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/working-with-string/112402/2 "2007-08-18T14:23:53Z")

</div>

> string. I also need to extract the file type i.e “gif” from  
> it in a separate string. Can anybody tell me how to do it  
> using regular expressions in Ruby. I know this can be done  
> using String.gsub, but i am too bad at reg expressions. And  
> if someone can point me to good source for learning reg  
> expressions then it would be really helpful.
> 
> ## Thanks
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

irb(main):001:0\> File.basename(‘/path/file.ext’)  
=\> “file.ext”  
irb(main):002:0\> File.extname(‘/path/file.ext’)  
=\> “.ext”  
irb(main):003:0\>

HTH,

Felix

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [August 18, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/working-with-string/112402/3 "2007-08-18T15:01:57Z")

</div>

Hi,

irb(main):035:0\> “/foo/foo.gif”.split("/")[-1]  
=\> “foo.gif”  
irb(main):038:0\> “/foo/foo.gif”.split(".")[-1]  
=\> “gif”

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [August 18, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/working-with-string/112402/4 "2007-08-18T15:32:44Z")

</div>

Thanks Gavin that really helped.

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [August 18, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/working-with-string/112402/5 "2007-08-18T15:11:58Z")

</div>

On Aug 18, 6:03 am, Dipesh B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi need some help with string manipulation. I have a string representing  
> full path of a file. Example “/foo/foo.gif”. I need to just extract the  
> file name i.e “foo.gif” from this string. I also need to extract the  
> file type i.e “gif” from it in a separate string. Can anybody tell me  
> how to do it using regular expressions in Ruby. I know this can be done  
> using String.gsub, but i am too bad at reg expressions. And if someone  
> can point me to good source for learning reg expressions then it would  
> be really helpful.

irb(main):001:0\> s = “/foo/bar/whee.gif”  
=\> “/foo/bar/whee.gif”  
irb(main):002:0\> s[%r{[^/]+$} ]  
=\> “whee.gif”  
irb(main):003:0\> s[%r{[^.]+$} ]  
=\> “gif”

Those expressions are:

- One or more characters (as many as you can grab) that are not  
forward slash characters, followed by an end-of-line marker.
- One or more characters (as many as you can grab) that are not period  
characters, followed by an end-of-line marker.

To save someone else from having to point it out, $ is not the end of  
string, but just the end of a line. You could use \z to anchor truly  
to the end of the string, but when I know the input will be single  
line, I prefer $ for simplicity and portability.

---

<div class="post-metadata">

**Author:** ![dipeshbatheja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b19c9b/32.png) [@dipeshbatheja](https://www.ruby-forum.com/u/dipeshbatheja)\
**Post date:** [August 18, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/working-with-string/112402/6 "2007-08-18T15:46:40Z")

</div>

Le samedi 18 aoÃ»t 2007 14:03, Dipesh B. a Ã©critÂ :

> Hi need some help with string manipulation. I have a string representing  
> full path of a file. Example “/foo/foo.gif”. I need to just extract the  
> file name i.e “foo.gif” from this string. I also need to extract the  
> file type i.e “gif” from it in a separate string. Can anybody tell me  
> how to do it using regular expressions in Ruby. I know this can be done  
> using String.gsub, but i am too bad at reg expressions. And if someone  
> can point me to good source for learning reg expressions then it would  
> be really helpful.
> 
> Thanks

You can also use a dedicated lib, for manipulating filenames : pathname

require ‘pathname’

p = Pathname.new “/foo/foo.gif”  
puts p.basename # =\> foo.gif  
puts p.extname # =\> “.gif”
