从文件中的行中提取双引号内的字符串

时间:2014-09-22 10:39:01

标签: perl

我的文件内容为:

Component (0463) "commonfiles"
Component (0464) "demo_new_comp"
Component (0467) "test_comp" (removed)
Component (0469) "test_comp3" (removed)
Component (0465) "textfiles1

需要从每行(已删除)中的双引号中提取字符串并放在数组中。 我的代码是:

my $fh = new IO::File;
$fh->open("<comp.log") or die "Cannot open comp.log";
my @comp_items;
while (<$fh>) {
    if ( $_ =~ /removed/ ) {;
        my $compName = $_ = ~ m/"(.*?)"/;
        print " Componnet Name : \"$compName\"\n";
    }
}

我没有得到正确的输出,给出一些数字:

"18446744073709551614"
"18446744073709551614"

输出应为:

test_comp
test_comp3

1 个答案:

答案 0 :(得分:7)

my $compName = $_ = ~ m/"(.*?)"/;

= ~=~不同,但是分配和bitwise negation

你想要的是什么,

my ($compName) = $_ =~ m/"(.*?)"/;

或只是,

my ($compName) = /"(.*?)"/;