Working with String

D

Dipesh Batheja

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
 
Y

Young Moon

Hi,

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

Phrogz

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.
 
O

Olivier Renaud

Le samedi 18 ao=C3=BBt 2007 14:03, Dipesh Batheja a =C3=A9crit=C2=A0:
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 =3D Pathname.new "/foo/foo.gif"
puts p.basename # =3D> foo.gif
puts p.extname # =3D> ".gif"

=2D-=20
Olivier Renaud
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,319
Latest member
LorenFlann

Latest Threads

Top