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:  <%-{-{-{-<


In reply to Re: searching files by AnomalousMonk
in thread searching files by michael99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.