Hi There Monks!

Trying to match file names with similar words on them, like these:

File name format samples:
20220401_note.txt 20220303_page.txt 20220101_page_with_blanks.txt 20220111_page_blanks.txt
These are the words to match on all the file names I am trying to get: ... my @get = qw(note page page_with_blanks page_blanks); # I have a sub to process them as they get read from a directory, code + not complete but to show where the issue is: sub get_count { my $dir = 'data'; my $match = join '|',@get; # Doing a for loop here to match according to the values in $match now +: my @files = glob "$dir/*.txt"; for my $file (sort @files) { # $match only gets the file names with "page" and ignores "page_w +ith_blanks" and "page_blank". # How could I match all the file names and the ones has "page" o +nly, also the other variations? if ($file =~ /^\bdata\b\/($year\d{4})_($match)/g) { my $date = $1; my $name = $2; print "D:$date - N:$name\n"; # It would print: #D: 20220401 - N:note.txt #D: 20220303 - N:page.txt #D: 20220101 - N:page_with_blanks.txt #D: 20220111 - N:page_blanks.txt }}

I hope its an easy way to explain the issue!
Thanks for looking!

In reply to Match similar words in file name by Anonymous Monk

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.