Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Expanding a perl regex to a list of files with full paths

Reply
Thread Tools

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

 
 
Neil Shadrach
Guest
Posts: n/a
 
      10-28-2003
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.

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      10-28-2003
Neil Shadrach <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Neil Shadrach
Guest
Posts: n/a
 
      10-28-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List all elements in xml with full paths fazl.rahman@volcanomail.com XML 11 06-24-2008 04:08 PM
Paths, gentleman, paths Ohad Lutzky Ruby 2 11-07-2006 02:15 AM
Expanding tree paths soup_or_power@yahoo.com Perl Misc 2 07-19-2005 06:43 PM
create foldertree from list of full paths, how? Bart Plessers \(artabel\) ASP General 0 09-30-2003 11:38 AM
Convert between Windows style paths and POSIX style paths Noah Python 5 07-11-2003 09:25 PM



Advertisments