Can't combine two regex statements to work at once

Sin*_*egy 0 regex perl filter

I have a simple perl code that displays the names of the files in a given directory

opendir my $dir, "./images/sampleImages" or die "Cannot open directory: $!";
my @Images = grep {!/^\./} readdir $dir;
closedir $dir;
Run Code Online (Sandbox Code Playgroud)

I have added a regex statement to remove all single and double dots, since readdir adds them as special characters and this is working perfectly fine. However I want only the files with the extensions of .jpg/.jpeg/.png/.gif to be read. I have found a regex that does that, which is :

(/\.(gif|jpg|jpeg|png)$/i)
Run Code Online (Sandbox Code Playgroud)

But I can't seem to make them work together. I have tried everything I could find on the subject here, combining the statements in different ways but either they give errors, or flat out do not work the way it should. Any help would be greatly appreciated, thanks in advance!

pmq*_*mqs 5

这行得通吗?

my @Images = grep { !/^\./ && /\.(gif|jpg|jpeg|png)$/i } readdir $dir;
Run Code Online (Sandbox Code Playgroud)

  • 您也可以使用 `grep { /^[^.].*\.(gif|jpg|jpeg|png)$/is }`,但我更喜欢 pmqs 稍慢但更易读的解决方案。 (2认同)