Perl を使用して、特定の単語が含まれる特定のフォルダー内のファイルの名前を取得しています。これらのファイル名のキーワードは、「offers」または「cleared」、「regup」または「regdn」です。つまり、"offers" または "cleared" のいずれか、および "regup" または "regdn" のいずれかがファイル名に含まれている必要があります。2 つの単語の順序は任意であり、その前後に文字/単語が表示されます。一致するファイル名の例は次のとおりです。
2day_Agg_AS_Offers_REGDN-09-JUN-11.csv
一致する各ファイル名をフルパスとして正常にキャプチャする正規表現がありますが、これは私が望んでいたものですが、エレガントで非効率的です。わずかに優れたコードの試みはすべて失敗しました。
作業アプローチ:
# Get the folder names
my @folders = grep /^\d{2}-/, readdir DIR;
foreach my $folder ( @folders ) {
# glob the contents of the folder (to get the file names)
my @contents = <$folder/*>;
# For each filename in the list, if it matches, print it
foreach my $item ( @contents ) {
if ($item =~ /^$folder(?=.*(offers|cleared))(?=.*(regup|regdn)).*csv$/i){
print "$item\n";
}
}
}
より短い/よりクリーンなものを試してください:
foreach my $folder ( @folders ) {
# glob the contents of the folder (to get the file names)
my @contents = <$folder/*>;
# Seems to determine that there are four matches in each folder
# but then prints the first matching filename four times
my $single = join("\n", @contents);
for ($single =~ /^$folder(?=.*(offers|cleared))(?=.*(regup|regdn)).*csv$/im) {
print "$&\n";#"Matched: |$`<$&>$'|\n\n";
}
}
他のオプション (/img、/ig など) を使用し、正規表現の出力を配列に送信して、正規表現で他の書式設定を試みましたが、何も正しく機能しませんでした。私は Perl が苦手なので、この手順全体をより効率的にする大きな機会を逃していると確信しています。ありがとう!