Ask a Question related to PERL Beginners, Design and Development.
-
Ben Crane #1
file path pattern matching problem.
Hi all,
I'm trying to split apart a filepath...e.g:
c:\test\abc\what\somefile.txt
The length of the filepath will never be constant...
e.g:
foreach $line (@Path_Filename)
{
chomp($line);
(@Path_Breakdown) = split(/(\w+\W)(\w+\W)/, $line);
}
but my biggest problem is how to match a word
character \w then match everything until the last
\...that will comprise of the file path and the final
\ onwards will be the filename incl. or excl. the file
extension...
I've tried to get the pattern matching to include
everything including the \ but it doesn't seem to
work. The closest I've gotten is:
c:\test\abc\what\somefile.txt
c:
\test\abc\what\
somefile.
txt
Any ideas? Is there a pattern character that I'm
missing here that allows you to match a certain
character and then stop if it's the last one of it's
type?
Ben
__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
[url]http://photos.yahoo.com/[/url]
Ben Crane Guest
-
pattern matching
brian liu <xliu75@yahoo.com> wrote: or $defaultDir='d:/haha'; or -
Pattern Matching Operators
Is there a similar operator in Perl for ${variable##pattern} as there is in korn shell. Thanks for the help. Prasad ... -
Pattern Matching in file with invisible char
Hi there, First of all, I am sorry if this is a FAQ. I couldn't find anything in the PerlDoc, so if I missed something please be kind enough to... -
[PHP] mysql Pattern Matching
From: "Ralph Guzman" <ralph213@sbcglobal.net> Actually no, I guess there isn't. Neither one will use an index. mysql> desc test;... -
mysql Pattern Matching
i'm doin this offlist -----Original Message----- From: Ralph Guzman Sent: Thursday, September 04, 2003 10:23 PM To: PHP General Mailing List... -
Jess Balint #2
RE: file path pattern matching problem.
Ben -
You can use the File::Basename module for this:
Your program would be akin to:
foreach $line (@Path_Filename)
{
chomp($line);
$filename = basename($line); # gives you the filename with the
extension
$location = dirname($line); # gives you the location with no
trailing /
}
Here are some examples:
~% perl -MFile::Basename -e'print basename($ARGV[0])' /etc/hosts.equiv
hosts.equiv
~% perl -MFile::Basename -e'print dirname($ARGV[0])' /etc/hosts.equiv
/etc
~ Jess
> -----Original Message-----
> From: Ben Crane [mailto:crane_ba@yahoo.com]
> Sent: Wednesday, December 10, 2003 9:22 AM
> To: [email]beginners@perl.org[/email]
> Subject: file path pattern matching problem.
>
>
> Hi all,
>
> I'm trying to split apart a filepath...e.g:
> c:\test\abc\what\somefile.txt
> The length of the filepath will never be constant...
>
> e.g:
> foreach $line (@Path_Filename)
> {
> chomp($line);
> (@Path_Breakdown) = split(/(\w+\W)(\w+\W)/, $line);
> }
>
> but my biggest problem is how to match a word
> character \w then match everything until the last
> \...that will comprise of the file path and the final
> \ onwards will be the filename incl. or excl. the file
> extension...
>
> I've tried to get the pattern matching to include
> everything including the \ but it doesn't seem to
> work. The closest I've gotten is:
>
> c:\test\abc\what\somefile.txt
> c:
> \test\abc\what\
> somefile.
> txt
>
> Any ideas? Is there a pattern character that I'm
> missing here that allows you to match a certain
> character and then stop if it's the last one of it's
> type?
>
> Ben
>
>
> __________________________________
> Do you Yahoo!?
> New Yahoo! Photos - easier uploading and sharing.
> [url]http://photos.yahoo.com/[/url]
>
> --
> To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
> For additional commands, e-mail: [email]beginners-help@perl.org[/email]
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>Jess Balint Guest
-
John W. Krahn #3
Re: file path pattern matching problem.
Ben Crane wrote:
Hello,>
> Hi all,
> I'm trying to split apart a filepath...e.g:
> c:\test\abc\what\somefile.txt
> The length of the filepath will never be constant...
$ perl -le'
use File::Spec;
my $path = q[c:\test\abc\what\somefile.txt];
my ( $vol, $dir, $file ) = File::Spec->splitpath( $path );
print qq[ "$vol" "$dir" "$file" ];
my @dirs = File::Spec->splitdir( $dir );
print map qq[ "$_" ], @dirs;
'
"c:" "\test\abc\what\" "somefile.txt"
"" "test" "abc" "what" ""
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Tom Kinzer #4
RE: file path pattern matching problem.
Yes! And use Basename too.
these will also give you the advantage of making your programs more
portable!
-Tom Kinzer
-----Original Message-----
From: John W. Krahn [mailto:krahnj@acm.org]
Sent: Wednesday, December 10, 2003 11:37 AM
To: [email]beginners@perl.org[/email]
Subject: Re: file path pattern matching problem.
Ben Crane wrote:Hello,>
> Hi all,
> I'm trying to split apart a filepath...e.g:
> c:\test\abc\what\somefile.txt
> The length of the filepath will never be constant...
$ perl -le'
use File::Spec;
my $path = q[c:\test\abc\what\somefile.txt];
my ( $vol, $dir, $file ) = File::Spec->splitpath( $path );
print qq[ "$vol" "$dir" "$file" ];
my @dirs = File::Spec->splitdir( $dir );
print map qq[ "$_" ], @dirs;
'
"c:" "\test\abc\what\" "somefile.txt"
"" "test" "abc" "what" ""
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Tom Kinzer Guest
-
B. Fongo #5
Re: file path pattern matching problem.
The best way to do it; is using the standard module File::Basename.
For instance
use File::Basename;
# This should return "somefile".
$file_name = basename (c:\test\abc\what\somefile.txt);
# This should also return "c:\test\abc\what\"
$dir_name = dir (c:\test\abc\what\somefile.txt);
# fileparse should returns file, directory and suffix.
($filename, $dir, $suffix) = fileparse (c:\test\abc\what\somefile.txt);
Check perldocs for details.
HTH
Babs
Ben Crane wrote:
>Hi all,
>
>I'm trying to split apart a filepath...e.g:
>c:\test\abc\what\somefile.txt
>The length of the filepath will never be constant...
>
>e.g:
>foreach $line (@Path_Filename)
> {
> chomp($line);
>(@Path_Breakdown) = split(/(\w+\W)(\w+\W)/, $line);
> }
>
>but my biggest problem is how to match a word
>character \w then match everything until the last
>\...that will comprise of the file path and the final
>\ onwards will be the filename incl. or excl. the file
>extension...
>
>I've tried to get the pattern matching to include
>everything including the \ but it doesn't seem to
>work. The closest I've gotten is:
>
>c:\test\abc\what\somefile.txt
>c:
>\test\abc\what\
>somefile.
>txt
>
>Any ideas? Is there a pattern character that I'm
>missing here that allows you to match a certain
>character and then stop if it's the last one of it's
>type?
>
>Ben
>
>
>__________________________________
>Do you Yahoo!?
>New Yahoo! Photos - easier uploading and sharing.
>[url]http://photos.yahoo.com/[/url]
>
>
>
B. Fongo Guest



Reply With Quote

