> Or better, how do I learn to use regex?

You have to read "Mastering Regular Expressions" if you wanna
be an expert, but you don't have to if all you want is to start
studying regexes. There are much simpler ways :

1) "Learning Perl" has a chapter about regular expressions
2) perldoc perlre

As about your question - the given split solution looks the good one.
I'll just let myself to correct it a bit :

> $location = "/blabla/dir/nextdir/name.txt";
> @dirs = split("\/", $location);

@dirs = split( m@[\\/]@, $location) - after Billy came up with strange
idea to use \ as a delimiter we have always to remember it

> print $dirs[$#dirs];

$dirs[ -1 ] looks better, I think ;)

Finally, File::Basename already does these things for you :

C:\>perl -w
use File::Basename;
my ( $name, $path ) = fileparse( '\blabla\dir/nextdir\name.txt' );
print $name, "\n";
print $path, "\n";

^Z
name.txt
\blabla\dir/nextdir\

C:\>


Reply via email to