我正在尝试用 perl 解析文件。我想在正则表达式匹配后打印所有行
例如,文件是
num_of_dogs,10,#start_reading
num_of_cat,15
num_birds,20
num_of_butterfly,80
.....
我想要比赛后的所有行
#start_reading
我试过这个,但它只是打印下一行
while (my $line = <$csv_file>) {
next unless $line =~ /(.*),#end_of_tc/;
if ($line =~ /(.*)/){
print $file = $1;
}
}
输出看起来像这样
num_of_cats,15
num_of_birds,20
......
提前致谢
最佳答案
当该行包含 #start_reading
时,您可以设置标志并且仅在标志为真时才打印该行:
while (my $line = <$csv_file>) {
print $line if $start;
$start ||= $line =~ /#start_reading/;
}
而如果遇到
#stop_reading
后想停止阅读:while (my $line = <$csv_file>) {
print $line if $print;
$print ||= $line =~ /#start_reading/;
$print &&= $line !~ /#stop_reading/;
}
关于regex - Perl : Print all lines after match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55582592/