perl – 从文件中的行引用双引号内的字符串

前端之家收集整理的这篇文章主要介绍了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

解决方法

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

=〜与=〜不同,但是赋值和bitwise negation

而你想要的是,

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

要不就,

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

猜你在找的Perl相关文章