Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Match similar words in file name

by choroba (Cardinal)
on Apr 06, 2022 at 18:06 UTC ( [id://11142746]=note: print w/replies, xml ) Need Help??


in reply to Match similar words in file name

The common trick is to sort the words by length descending. The longer matches will then be tried first.
#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; my @files = qw( 20220401_note.txt 20200101_no_match.txt 20220303_page.txt 20220101_page_with_blanks.txt 20220111_page_blanks.txt ); my @get = qw( note page page_with_blanks page_blanks ); my $match = join '|', sort { length $b <=> length $a } map quotemeta, @get; for my $file (@files) { if (my ($date, $name) = $file =~ /(202[0-9]{5})_($match)/) { say "D:$date - N:$name"; } }

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found