我的文件内容如下:
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) = /"(.*?)"/;