Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: searching files

by AnomalousMonk (Bishop)
on Mar 05, 2020 at 09:23 UTC ( #11113832=note: print w/replies, xml ) Need Help??


in reply to searching files

If you don't want to follow the advice of others concerning Path::* (and there are other modules to do similar things) and want to roll your own regex, be warned that regex be tricky.

For instance, the  qr/${user_input}_ABC_.*pd/ regex used here fails (i.e., accepts as valid) the xyzabc_ABC_hipdip filename. (There's also the problem that the logical sense of the test is wrong, but that's minor.)

Line-end anchors  \A \z can be useful here, as can  \Q \E metaquoting escape sequences to guard against regex metacharacters included in the  $user_input portion of the filename. E.g.:

c:\@Work\Perl\monks>perl -wMstrict -le "my $user_input = 'ab+c'; my $excluded = qr{ \A \Q$user_input\E _ABC_ .* [.] pdf \z }xms; ;; FILE: foreach my $file (qw( ab+c_ABC_something.pdf ab+c_ABC_anything.pdf ab+c_ABC_.pdf xab+c_ABC_.pdf ab+c_ABC_.pdfx xab+c_ABC_.pdfx abc_ABC_something.pdf abc_ABC_anything.pdf abc_ABC_.pdf abc _.pdf xyzabc_ABC_hipdip foobar )) { next FILE if $file =~ $excluded; print qq{'$file' is not excluded}; } " 'xab+c_ABC_.pdf' is not excluded 'ab+c_ABC_.pdfx' is not excluded 'xab+c_ABC_.pdfx' is not excluded 'abc_ABC_something.pdf' is not excluded 'abc_ABC_anything.pdf' is not excluded 'abc_ABC_.pdf' is not excluded 'abc' is not excluded '_.pdf' is not excluded 'xyzabc_ABC_hipdip' is not excluded 'foobar' is not excluded
Note that the initialization of the  $user_input has been changed to  'ab+c' so as to include a regex metacharacter as an example of the effect of the  \Q \E escapes.

Update: The quotemeta built-in can be used instead of  \Q \E escapes:
    my $user_input = quotemeta 'ab+c';
    my $excluded = qr{ \A $user_input _ABC_ .* [.] pdf \z }xms;

Note also that thorough testing is wise in case you are using a regex approach; see Test::More and its ilk.

Please see perlre, perlretut, and perlrequick.


Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11113832]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2020-06-07 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (42 votes). Check out past polls.

    Notices?