Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Perl regex在目录中列出带有.c和.h的文件_Regex_Perl - Fatal编程技术网

如何使用Perl regex在目录中列出带有.c和.h的文件

如何使用Perl regex在目录中列出带有.c和.h的文件,regex,perl,Regex,Perl,如何列出目录中包含.c和.h文件的文件。 目前我只使用ifm/\.c$/it提供.c文件。我需要用于.c和.h文件的正则表达式。使用字符类。也就是说,将c和h放在方括号内 if(m/.\.[hc]$/) [hc]匹配h或c在Perl中,您可以使用以下代码列出目录中扩展名为.c和.h的所有文件: opendir my $dir, "some/path/" or die "Cannot open directory: $!"; my @files = grep{/\.c$|\.h$/}readdi

如何列出目录中包含.c和.h文件的文件。
目前我只使用ifm/\.c$/it提供.c文件。我需要用于.c和.h文件的正则表达式。

使用字符类。也就是说,将c和h放在方括号内

if(m/.\.[hc]$/)

[hc]匹配h或c在Perl中,您可以使用以下代码列出目录中扩展名为.c和.h的所有文件:

opendir my $dir, "some/path/" or die "Cannot open directory: $!";
my @files = grep{/\.c$|\.h$/}readdir $dir;
closedir $dir;
print "@files";
编辑 如果你真的想使用If,你可以这样做:

opendir my $dir, "some/path/" or die "Cannot open directory: $!";
my @files = readdir $dir;
closedir $dir;
foreach (@files)
{
    if (/.\.[ch]\z/s)
    {
        print "$_\n";
    }
}

要获取目录中的文件列表,请使用glob。见perldoc-f glob

如果路径或文件名中有空格,请使用bsd_glob from file::glob

请参阅perldoc文件::Glob


Glob是一个标准模块,与Perl一起安装。有关所有标准模块的列表,请参见perldoc perlmodlib

Hi,我想在if/\.c\z\.h\z/或/\.[ch]\z/,实际上/\.[ch]\z/s更好,因为它与名为.c或.h.m/\.[hc]\z/s的文件不匹配,actually@ikegami这里需要s修饰语吗?那么。匹配任何字符。
my @files = glob( "$path/*.[ch]" );
print "$path : @files\n";
use File::Glob qw( :bsd_glob );
my @files = glob( "$path/*.[ch]" );
print "$path : @files\n";