Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
Regex 如何在perl中的正则表达式中包含grep_Regex_Perl_Grep - Fatal编程技术网

Regex 如何在perl中的正则表达式中包含grep

Regex 如何在perl中的正则表达式中包含grep,regex,perl,grep,Regex,Perl,Grep,因此,我目前被困在这个问题上: 1.我声明一个常量列表,比如列表 2.我想读取一个文件,我在while循环中逐行读取,如果该行有一个来自列表的关键字,我会打印该行,或者打印其他内容 这就是我目前拥有的: use constant LIST => ('keyword1', 'keyword2', 'keyword3'); sub main{ unless(open(MYFILE, $file_read)){die "Error\n"}; while(<MYFILE>

因此,我目前被困在这个问题上: 1.我声明一个常量列表,比如列表 2.我想读取一个文件,我在while循环中逐行读取,如果该行有一个来自列表的关键字,我会打印该行,或者打印其他内容

这就是我目前拥有的:

use constant LIST => ('keyword1', 'keyword2', 'keyword3');
sub main{
    unless(open(MYFILE, $file_read)){die "Error\n"};
    while(<MYFILE>){
        my $line = $_;
        chomp($line);
        if($line =~ m//){#here is where i'm stuck, i want is if $line has either of the keywords
            print $line;
        }
    }
}
使用常量列表=>('keyword1','keyword2','keyword3');
副总管{
除非(打开(MYFILE,$file_read)){die“Error\n”};
while(){
我的$line=$\ux;
chomp($line);
如果($line=~m/){#这里是我被卡住的地方,我想知道的是$line是否有任何一个关键字
打印$行;
}
}
}
我应该在if语句中做什么来匹配我希望程序做的事情?我是否可以不使用
$行
变量并简单地使用
$\uu
?我只使用了$line,因为我认为grep会自动将列表中的常量放入
$\uu

谢谢

最简单的方法是将带引号的正则表达式定义为常量,而不是列表:

use strict;
use warnings;
use autodie;    # Will kill program on bad opens, closes, and writes
use feature qw(say);   # Better than "print" in most situations

use constant {
   LIST => qr/keyword1|keyword2|keyword3/, # Now a regular expression.
   FILE_READ => 'file.txt', # You're defining constants, make this one too.
};

open my $read_fh, "<", FILE_READ;  # Use scalars for file handles

# This isn't Java. You don't have to define "main" subroutine

while ( my $line = <$read_fh> ) {
    chomp $line;
    if ( $line =~ LIST ) {  #Now I can use the constant as a regex
        say $line;
    }
}
close $read_fh;
如果必须使用列表作为常量,则可以使用
join
生成正则表达式:

use constant LIST => qw( keyword1 keyword2 keyword3 );

...

my $regex = join "|", map LIST;
while ( my $line = <$file_fh> ) {
    chomp $line;
    if ( $line =~ /$regex/ ) {
        say $line;
    }
}
这对初学者来说有点难理解<代码>映射获取
列表中的每个元素
并对其运行命令。这将返回我分配给
@list
的列表

想象一下:

use constant LIST => qw( periods.are special.characters in.regular.expressions );
当我跑步时:

my @list = map { quotemeta } LIST;
my $regex = join "|", @list;
这将返回以下列表:

my @list = ( "periods\.are", "special\.characters", "in\.regular\.expressions" );
现在,句点是文字句点,而不是正则表达式中的特殊字符。当我跑步时:

my @list = map { quotemeta } LIST;
my $regex = join "|", @list;
我得到:

$regex = "periods\.are|special\.characters|in\.regular\.expressions";

这是一个有效的正则表达式。

@Zaid我以前不知道
quotemeta
命令。这很有效。