file path pattern matching problem.

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. pattern matching
      brian liu <xliu75@yahoo.com> wrote: or $defaultDir='d:/haha'; or
    2. Pattern Matching Operators
      Is there a similar operator in Perl for ${variable##pattern} as there is in korn shell. Thanks for the help. Prasad ...
    3. 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...
    4. [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;...
    5. 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...
  3. #2

    Default 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

  4. #3

    Default Re: file path pattern matching problem.

    Ben Crane wrote:
    >
    > Hi all,
    Hello,
    > 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

  5. #4

    Default 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:
    >
    > Hi all,
    Hello,
    > 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

  6. #5

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139