Regex for splitting filenames

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

···

--
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)])

···

On Wed, Jun 07, 2006 at 10:30:23AM +0900, darren kirby wrote:

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

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

-austin

···

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?

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

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

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

-a

···

On Wed, 7 Jun 2006, darren kirby wrote:

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

--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

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]

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

···

On 6/7/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?

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

···

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)

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

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

-austin

···

On 6/6/06, Matthew Smillie <M.B.Smillie@sms.ed.ac.uk> wrote:

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

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca