U
usenet
This sounds easy, but it has puzzled me. I want to parse a filename
into "path" , "basename", and "suffix" where terms are defined thus:
PATH - everything up to the last (or only) forward-slash.
SUFFIX - anything after the last (or only) dot
BASENAME - all between the path and the dot before the suffix.
Disgard the trailing slash in the path and the dot before the suffix.
It may be assumed that the parser will only process names of plain
files (so '/foo' is a file named 'foo' in '/', not a directory).
The parser should work if the filename has no path and/or no suffix
(those values should resolve to undef if not present in the filename).
I don't know what suffixes it might encounter (so using File::Basename
is not so obvious, though I've tried a qr// without much success,
because I can't figure out how to curb the greediness of the
expressions in this context).
I've been playing around with some code within this test framework
using multiple possible styles of filenames:
#!/usr/bin/perl
use strict;
use File::Basename;
foreach my $file(<DATA>) {chomp $file;
#my ($path, $name, $suffix) =
($file =~ m!^(?.*)/)?(.*)(?:\.(.*))?$!);
#my ($name,$path,$suffix) = fileparse($file, qr{\..*});
#Gotta come up with SOMETHING here!!!
printf ("%-19s%-7s%-9s%-4s\n", $file, $path, $name, $suffix);
}
__DATA__
/PATH/NAME.SUFFIX
/foo
/foo.txt
/tmp.xyz/foo.txt
tmp.xyz/foo.bar.txt
/tmp.xyz/foo
tmp/foo.txt
../tmp/foo.bar.txt
/tmp/foo
foo.txt
foo.bar.txt
foo
###### DESIRED OUTPUT #########################
/PATH/NAME.SUFFIX /PATH NAME SUFFIX
/foo / foo
/foo.txt / foo txt
/tmp.xyz/foo.txt /tmp.xyz foo txt
tmp.xyz/foo.bar.txt tmp.xyz foo.bar txt
/tmp.xyz/foo /tmp.zyz foo
/tmp/foo.txt /tmp foo txt
../tmp/foo.bar.txt ./tmp foo.bar txt
/tmp/foo /tmp foo
foo.txt foo txt
foo.bar.txt foo.bar txt
foo foo
I can ALMOST get it to work, but not quite... if I fix one test case, I
break another. I appreciate any suggestions...
into "path" , "basename", and "suffix" where terms are defined thus:
PATH - everything up to the last (or only) forward-slash.
SUFFIX - anything after the last (or only) dot
BASENAME - all between the path and the dot before the suffix.
Disgard the trailing slash in the path and the dot before the suffix.
It may be assumed that the parser will only process names of plain
files (so '/foo' is a file named 'foo' in '/', not a directory).
The parser should work if the filename has no path and/or no suffix
(those values should resolve to undef if not present in the filename).
I don't know what suffixes it might encounter (so using File::Basename
is not so obvious, though I've tried a qr// without much success,
because I can't figure out how to curb the greediness of the
expressions in this context).
I've been playing around with some code within this test framework
using multiple possible styles of filenames:
#!/usr/bin/perl
use strict;
use File::Basename;
foreach my $file(<DATA>) {chomp $file;
#my ($path, $name, $suffix) =
($file =~ m!^(?.*)/)?(.*)(?:\.(.*))?$!);
#my ($name,$path,$suffix) = fileparse($file, qr{\..*});
#Gotta come up with SOMETHING here!!!
printf ("%-19s%-7s%-9s%-4s\n", $file, $path, $name, $suffix);
}
__DATA__
/PATH/NAME.SUFFIX
/foo
/foo.txt
/tmp.xyz/foo.txt
tmp.xyz/foo.bar.txt
/tmp.xyz/foo
tmp/foo.txt
../tmp/foo.bar.txt
/tmp/foo
foo.txt
foo.bar.txt
foo
###### DESIRED OUTPUT #########################
/PATH/NAME.SUFFIX /PATH NAME SUFFIX
/foo / foo
/foo.txt / foo txt
/tmp.xyz/foo.txt /tmp.xyz foo txt
tmp.xyz/foo.bar.txt tmp.xyz foo.bar txt
/tmp.xyz/foo /tmp.zyz foo
/tmp/foo.txt /tmp foo txt
../tmp/foo.bar.txt ./tmp foo.bar txt
/tmp/foo /tmp foo
foo.txt foo txt
foo.bar.txt foo.bar txt
foo foo
I can ALMOST get it to work, but not quite... if I fix one test case, I
break another. I appreciate any suggestions...