On 7/18/2004 4:14 AM, Zysman, Roiy wrote:

Hi all,

Is there a way to extract a filename out of a path string using just one
RegEx and not using the split function ?

For Example, I want to extract foo.txt from /my/long/path/foo.txt path
string.

use File::Basename; -or- use File::Spec;

Both are core modules. Both are the safest way to do what you want.

But you'll often see it done this way:

my $path = '/my/long/path/foo.txt';

(my $file = $path) =~ s/^.*\///;
 -or-
(my $file) = $path =~ /([^\/]+)$/;

or similar. But you should really use

use File::Basename;
my $file = basename( $path );
 -or-
my $file = (File::Spec->splitpath( $path ))[2];

Randy.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to