Forum: Ruby regex for splitting filenames

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0cb023654f46a6bd171405af6419baf6?d=identicon&s=25 darren kirby (Guest)
on 2006-06-07 03:32
(Received via mailing list)
Hello all,

I want to split a filename into it's root and extension, ie:
someFileName.txt = 'someFileName' and 'txt'

This is simple enough with string.split(), but what if the file has more
than
one period in it? I have worked around this by doing:

filename = "some.file.name.txt"
temp = filename.split(".")
type = temp.pop
fileroot = temp.join(".")

I was wondering though if instead of the temp variable I could just do:

fileroot, type = filename.split(/regex here?/)

I cannot find a way to write a regex that only matches the last period
in the
filename. Is there an elegant way to do this?

Thanks,
-d
B2156f905dd13ec3f45123fbe0dc0f8f?d=identicon&s=25 unknown (Guest)
on 2006-06-07 03:41
(Received via mailing list)
On Wed, Jun 07, 2006 at 10:30:23AM +0900, darren kirby wrote:
> type = temp.pop
> -d
> --
> darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
> "...the number of UNIX installations has grown to 10, with more expected..."
> - Dennis Ritchie and Ken Thompson, June 1972

base = ("#{filename}"[0..("#{filename}".rindex('.')-1)])
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-06-07 03:56
(Received via mailing list)
On 6/6/06, darren kirby <bulliver@badcomputer.org> wrote:
> fileroot, type = filename.split(/regex here?/)

> I cannot find a way to write a regex that only matches the last period in the
> filename. Is there an elegant way to do this?

  ext = File.extname(filename)
  file = File.basename(filename, ext)

-austin
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-06-07 03:59
(Received via mailing list)
On Wed, 7 Jun 2006, darren kirby wrote:

> type = temp.pop
> -d
jib:~ > ruby -e'  p "bar.txt".split( %r/\.([^\.]+)$/ )  '
   ["bar", "txt"]



   jib:~ > ruby -e'  p "/foo/bar.txt".split( %r/\.([^\.]+)$/ )  '
   ["/foo/bar", "txt"]


-a
31af45939fec7e3c4ed8a798c0bd9b1a?d=identicon&s=25 Matthew Smillie (notmatt)
on 2006-06-07 04:06
(Received via mailing list)
On Jun 7, 2006, at 2:54, Austin Ziegler wrote:

> On 6/6/06, darren kirby <bulliver@badcomputer.org> wrote:
>> fileroot, type = filename.split(/regex here?/)
>
>> I cannot find a way to write a regex that only matches the last
>> period in the
>> filename. Is there an elegant way to do this?
>
>  ext = File.extname(filename)
>  file = File.basename(filename, ext)

Equivalent, but I like the semantics a little better

require 'pathname'
f = Pathname.new(filename)
f.basename
f.extname


pathname is very handy - see the standard library docs: http://ruby-
doc.org/stdlib/libdoc/pathname/rdoc/index.html
0cb023654f46a6bd171405af6419baf6?d=identicon&s=25 darren kirby (Guest)
on 2006-06-07 04:12
(Received via mailing list)
quoth the Austin Ziegler:
> On 6/6/06, darren kirby <bulliver@badcomputer.org> wrote:
> > fileroot, type = filename.split(/regex here?/)
> >
> > I cannot find a way to write a regex that only matches the last period in
> > the filename. Is there an elegant way to do this?
>
>   ext = File.extname(filename)
>   file = File.basename(filename, ext)
>
> -austin

Thanks. I should have guessed there would be a builtin for it...

-d
1c1e3bdfe006a22214102fcd6434a012?d=identicon&s=25 Daniel Sheppard (Guest)
on 2006-06-07 04:21
(Received via mailing list)
> I was wondering though if instead of the temp variable I
> could just do:
>
> fileroot, type = filename.split(/regex here?/)
>
> I cannot find a way to write a regex that only matches the
> last period in the
> filename. Is there an elegant way to do this?

Not sure if it can be considered elegant, or if it'll work in all
situations, but:

fileroot, type = /^([^.]*$|.*(?=\.))\.?(.*)$/.match(filename)[1..2]

Seems to work for the tests I can think of:

fdasfa              => ["fdasfa", ""]
.fdsafds            => ["", "fdsafds"]
dda.dfasd           => ["dda", "dfasd"]
fdsafd.fdsdaf.fdsaf => ["fdsafd.fdsdaf", "fdsaf"]
fdasfas.            => ["fdasfas", ""]

Alternatively, the following might be easier to read:

fileroot, type = (/(.*)(\..*)/.match(filename)||[nil,filename])[1..2]
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-06-07 14:05
(Received via mailing list)
On 6/6/06, Matthew Smillie <M.B.Smillie@sms.ed.ac.uk> wrote:
>
> Equivalent, but I like the semantics a little better
>
> require 'pathname'
> f = Pathname.new(filename)
> f.basename
> f.extname

> pathname is very handy - see the standard library docs: http://ruby-
> doc.org/stdlib/libdoc/pathname/rdoc/index.html

I think you mean f.basename(f.extname) for the basename without the
extensions.

-austin
890028bf839dd5e0005612d44d0bbab1?d=identicon&s=25 Harry (Guest)
on 2006-06-07 16:30
(Received via mailing list)
If you want to use regex, try this;


str = "abc.def.ghi.jkl.mno"
root, ext = /^.*\./.match(str).to_s.chop, /^.*\./.match(str).post_match


Harry
This topic is locked and can not be replied to.