Expanding a perl regex to a list of files with full paths

Discussion in 'Perl Misc' started by Neil Shadrach, Oct 28, 2003.

  1. I have a situation where I'd like to specify a list of files ( full paths )
    with a perl regular expression as opposed to shell wildcards.
    Something like qr!/usr/path[abc]/file[123]|/user/[abc]path/longer/file[456]!
    If a list of all files on the machine ( find / -print ) was cheap to get that
    would be fine but it isn't since I will want to check the expansion at intervals.
    A reasonable compromise would seem to be to allow individual directory or file names
    to be regular expressions but for the separators to be fixed.
    That is for qr!/usr/path[abc]/file[123]! I could open each directory in turn and pick
    out matches. For my first example I'd have to do it as separately
    qr!/usr/path[abc]/file[123]! and qr!/user/[abc]path/longer/file[456]!

    Is there a better way to do this? I'd really like to be able to use a single regular expression
    with no restrictions ( and without the need to split on the directory separator ).
    I've looked at File::Find but couldn't see how it could be used for this case.
    Neil Shadrach, Oct 28, 2003
    #1
    1. Advertising

  2. Neil Shadrach

    Anno Siegel Guest

    Neil Shadrach <> wrote in comp.lang.perl.misc:
    > I have a situation where I'd like to specify a list of files ( full paths )
    > with a perl regular expression as opposed to shell wildcards.
    > Something like qr!/usr/path[abc]/file[123]|/user/[abc]path/longer/file[456]!
    > If a list of all files on the machine ( find / -print ) was cheap to get that
    > would be fine but it isn't since I will want to check the expansion at
    > intervals.
    > A reasonable compromise would seem to be to allow individual directory
    > or file names
    > to be regular expressions but for the separators to be fixed.
    > That is for qr!/usr/path[abc]/file[123]! I could open each directory in
    > turn and pick
    > out matches. For my first example I'd have to do it as separately
    > qr!/usr/path[abc]/file[123]! and qr!/user/[abc]path/longer/file[456]!
    >
    > Is there a better way to do this? I'd really like to be able to use a
    > single regular expression
    > with no restrictions ( and without the need to split on the directory
    > separator ).
    > I've looked at File::Find but couldn't see how it could be used for this case.


    Why not? $File::Find::name contains the complete path, you can match
    against that.

    In general, there is no other way but to dive into *all* directories
    looking for a match. With a general regex you won't be able to tell
    from looking at a directory name whether the directory can hold
    matching files or not. Just think of /^a.*a$/ or even /^(.).*\1/.

    So, unless you can divide the regex into parts that match individual
    path components, you will have to walk the entire directory tree.

    Anno
    Anno Siegel, Oct 28, 2003
    #2
    1. Advertising

  3. Anno Siegel wrote:

    > Why not? $File::Find::name contains the complete path, you can match
    > against that.


    True. I suppose what I rather vaguely had in mind was a way of using the module that
    would optimize the search. That is not go down paths that could not possibly
    match - if this could be determined from the expression. This implies breaking it
    down in some way I guess. I did this by splitting on '/' but of course I've
    lost the regex then. May be my best practical option.

    > In general, there is no other way but to dive into *all* directories
    > looking for a match. With a general regex you won't be able to tell
    > from looking at a directory name whether the directory can hold
    > matching files or not. Just think of /^a.*a$/ or even /^(.).*\1/.
    >
    > So, unless you can divide the regex into parts that match individual
    > path components, you will have to walk the entire directory tree.


    That's the conclusion I came to but I've been wrong before so I thought I'd ask :)

    Thanks

    Neil Shadrach
    Neil Shadrach, Oct 28, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    769
  2. Replies:
    11
    Views:
    2,412
    Joseph J. Kesselman
    Jun 24, 2008
  3. Bart Plessers \(artabel\)

    create foldertree from list of full paths, how?

    Bart Plessers \(artabel\), Sep 30, 2003, in forum: ASP General
    Replies:
    0
    Views:
    110
    Bart Plessers \(artabel\)
    Sep 30, 2003
  4. Ohad Lutzky

    Paths, gentleman, paths

    Ohad Lutzky, Nov 6, 2006, in forum: Ruby
    Replies:
    2
    Views:
    185
    David Vallner
    Nov 7, 2006
  5. Expanding tree paths

    , Jul 19, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    78
Loading...

Share This Page