From: Daniel Sheppard Date: 2006-06-07T11:19:21+09:00 Subject: Re: regex for splitting filenames > 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]