Get filename from path

Posted by Eric on Stack Overflow See other posts from Stack Overflow or by Eric
Published on 2010-05-18T16:10:13Z Indexed on 2010/05/18 16:31 UTC
Read the original article Hit count: 223

Filed under:

I am trying to parse the filename from paths. I have this:

my $filepath = "/Users/Eric/Documents/foldername/filename.pdf";
$filepath =~ m/^.*\\(.*[.].*)$/;
print "Linux path:";
print $1 . "\n\n";
print "-------\n";

my $filepath = "c:\\Windows\eric\filename.pdf";
$filepath =~ m/^.*\\(.*[.].*)$/;
print "Windows path:";
print $1 . "\n\n";
print "-------\n";

my $filepath = "filename.pdf";
$filepath =~ m/^.*\\(.*[.].*)$/;
print "Without path:";
print $1 . "\n\n";
print "-------\n";

But that returns:

Linux path:

-------
Windows path:Windowsic
                      ilename.pdf

-------
Without path:Windowsic
                      ilename.pdf

-------

I am expecting this:

Linux path:
filename.pdf
-------
Windows path:
filename.pdf
-------
Without path:
filename.pdf
-------

Can somebody please point out what I am doing wrong?

Thanks! :)

© Stack Overflow or respective owner

Related posts about perl