# Working with String

**URL:** <https://rubytalk.org/t/working-with-string/40022>\
**Category:** ruby-talk\
**Created:** [18 August 2007 12:03 UTC](https://rubytalk.org/t/working-with-string/40022 "2007-08-18T12:03:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dipesh\_Batheja](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@Dipesh\_Batheja](https://rubytalk.org/u/Dipesh_Batheja)\
**Post date:** [18 August 2007 12:03 UTC](https://rubytalk.org/t/working-with-string/40022/1 "2007-08-18T12:03:18Z")

</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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Felix\_Windt](https://avatars.discourse-cdn.com/v4/letter/f/bc8723/32.png) [@Felix\_Windt](https://rubytalk.org/u/Felix_Windt)\
**Post date:** [18 August 2007 12:22 UTC](https://rubytalk.org/t/working-with-string/40022/2 "2007-08-18T12:22:24Z")

</div>

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

> **···**
>
> > -----Original Message-----  
> > From: list-bounce@example.com  
> > [mailto:list-bounce@example.com] On Behalf Of Dipesh Batheja  
> > Sent: Saturday, August 18, 2007 5:03 AM  
> > To: ruby-talk ML  
> > Subject: Working with String
> > 
> > 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  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Young\_Moon](https://avatars.discourse-cdn.com/v4/letter/y/8dc957/32.png) [@Young\_Moon](https://rubytalk.org/u/Young_Moon)\
**Post date:** [18 August 2007 12:59 UTC](https://rubytalk.org/t/working-with-string/40022/3 "2007-08-18T12:59:57Z")

</div>

Hi,

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

> **···**
>
> On Aug 18, 5:03 pm, Dipesh Batheja \<dipesh\_bath...@yahoo.com\> 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.
> > 
> > Thanks  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [18 August 2007 13:09 UTC](https://rubytalk.org/t/working-with-string/40022/4 "2007-08-18T13:09:58Z")

</div>

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.

> **···**
>
> On Aug 18, 6:03 am, Dipesh Batheja \<dipesh\_bath...@yahoo.com\> 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.

---

<div class="post-metadata">

**Author:** ![Olivier](https://avatars.discourse-cdn.com/v4/letter/o/d2c977/32.png) [@Olivier](https://rubytalk.org/u/Olivier)\
**Post date:** [18 August 2007 13:43 UTC](https://rubytalk.org/t/working-with-string/40022/5 "2007-08-18T13:43:33Z")

</div>

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"

> **···**
>
> Le samedi 18 août 2007 14:03, Dipesh Batheja 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
> 
> --  
> Olivier Renaud

---

<div class="post-metadata">

**Author:** ![Dipesh\_Batheja](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@Dipesh\_Batheja](https://rubytalk.org/u/Dipesh_Batheja)\
**Post date:** [18 August 2007 13:32 UTC](https://rubytalk.org/t/working-with-string/40022/6 "2007-08-18T13:32:44Z")

</div>

Thanks Gavin that really helped.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
