无法将两个正则表达式语句同时合并工作。

huangapple go评论135阅读模式
英文:

Can't combine two regex statements to work at once

问题

我有一段简单的Perl代码,用于显示给定目录中文件的名称。

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

我已经添加了一个正则表达式语句来删除所有单个和双重点,因为readdir将它们添加为特殊字符,而这部分已经正常运行。然而,我只想读取具有扩展名为.jpg/.jpeg/.png/.gif的文件。我找到了一个可以做到这一点的正则表达式,即:

(/\.(gif|jpg|jpeg|png)$/i)

但我似乎无法使它们一起工作。我已经尝试了在这方面找到的一切,以不同的方式组合语句,但要么它们会出现错误,要么根本不能按照应该的方式工作。非常感谢您提前的帮助!

英文:

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;

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)

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!

答案1

得分: 5

这有效吗?

my @Images = grep { !/^\./ && /\.(gif|jpg|jpeg|png)$/i } readdir $dir;
英文:

Does this work?

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

答案2

得分: 3

你可以使用多个匹配运算符:

my @dirs =
    grep { /\.(?:gif|jpe?g|png)\z/i && ! /\A\./ }
    readdir $dir;

通常,我会为每个想法创建一个 grep,以便可以单独禁用它们:

my @dirs =
    grep { /\.(?:gif|jpe?g|png)\z/i }
    grep { ! /\A\./ }
    readdir $dir;

而且,通常我会添加一个 map 来生成完整的路径,因为 readdir 不会为你这样做(通常我会先做错,然后再回来做这个):

my @dirs =
    map  { catfile( $dir, $_ ) }
    grep { /\.(?:gif|jpe?g|png)\z/ }
    grep { ! /\A\./ }
    readdir $dir;

另外,readdir 不会 "添加" ...。它们是虚拟目录,表示当前目录和父目录。如果这些是您想要删除的唯一内容,那么您可以改进部分内容以仅匹配这些内容,而不是所有隐藏文件:

my @dirs =
    map  { catfile( $dir, $_ ) }
    grep { /\.(?:gif|jpe?g|png)\z/ }
    grep { ! /\A\.\.?\z/ }
    readdir $dir;

再次提醒,尽管我知道并且已经写了无数次,但只有当我在输出中看到 ... 时,我才会添加它。有些事情永远不会改变。

英文:

You can use multiple match operators:

 my @dirs =
     grep { /\.(?:gif|jpe?g|png)\z/i && ! /\A\./ }
     readdir $dir;

I typically make one grep per idea so I can disable them individually:

 my @dirs =
     grep { /\.(?:gif|jpe?g|png)\z/i }
     grep { ! /\A\./ }
     readdir $dir;

And often I add in a map to make the full path since readdir doesn't do that for you (and usually I do it wrong first then come back to do this):

 my @dirs =
     map  { catfile( $dir, $_ ) }
     grep { /\.(?:gif|jpe?g|png)\z/ }
     grep { ! /\A\./ }
     readdir $dir;

And, readdir doesn't "add" . and ... Those are the virtual directories that mean the current directory and the parent directory. If those are the only things that you want to remove, then you can improve that part to match only those rather than all hidden files:

 my @dirs =
     map  { catfile( $dir, $_ ) }
     grep { /\.(?:gif|jpe?g|png)\z/ }
     grep { ! /\A\.\.?\z/ }
     readdir $dir;

Again, even though I know that and written that a million times, I still only add it once I see . and .. in the output. Some things never change.

答案3

得分: 0

以下是翻译好的代码部分:

使用严格模式;
使用警告;

使用功能 'say';

我的 $dirname = './images/sampleImages';

打开目录 my $dir, $dirname
    or die "无法打开目录:$dirname";

my @Images = grep { /\.(gif|jpg|jpeg|png)$/i } readdir $dir;

关闭目录 $dir;

map{ say } @Images;
英文:

Following code filters in only images, no need to look at . and .. at all

use strict;
use warnings;

use feature 'say';

my $dirname = './images/sampleImages';

opendir my $dir, $dirname 
	or die "Cannot open directory: $dirname";
	
my @Images = grep { /\.(gif|jpg|jpeg|png)$/i } readdir $dir;

closedir $dir;

map{ say } @Images;

huangapple
  • 本文由 发表于 2020年1月6日 19:48:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定