From: Austin Ziegler Date: 2005-07-28T21:30:54+09:00 Subject: Re: Newbie: unexpected result with string.split( regexpr ) On 7/28/05, francisrammeloo@hotmail.com wrote: > My goal is to obtain the filename out of a full pathname. > > This is the code: > fullname = "./Dir/file.txt" > # Searches greedyly until the last slash > array = fullname.split(/.*\//) > > The results are: > puts array.length ==> 2 > puts array[0] ==> (newline symbol or something??) > puts array[1] ==> file.txt > > My questions are: > Why has the array a length of 2? > What is in array[0]? Because you're splitting, not matching. ./Dir/file.txt ^^^^^^ <-- What's here is your separator. There's nothing *before* your separator, so array[0] is an empty string; array[1] is the filename. There are two ways that are better: fullname = "./Dir/file.txt" # Alternate regex form to avoid escaping. array = fullname.split(%r{/}, -1) puts array.length, array[-1] # Best way puts File.basename(fullname) -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca