Win8 Strawberry 5.8.9.5 (32) Wed 06/09/2021 15:25:55 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings use Data::Dump qw(dd); my $data = <<'EOD'; bist_ctlr_clk_bistctlr, bist_ctlr_rst_bistctlr_n, bist_ctlr_serial_in_system, bist_ctlr_serial_out_system, bist_ctlr_shift_en_system, bist_ctlr_global_bbad, bist_ctlr_global_repairable, bist_ctlr_bist_mode, bist_ctlr_mem_type_sel, bist_ctlr_mode_sel, bist_ctlr_memsafe, bist_ctlr_sif_reg_en_system, bist_ctlr_sif_load_en_system, bist_ctlr_sif_update_en_system, bist_ctlr_mem_atpg_mode, bist_ctlr_flash_data_select, bist_ctlr_flash_repair_data, bist_ctlr_bend, EOD open (my $ifh51, '<', \$data) or die $!; my $rx_match = qr{ bist_mode | mode_sel | mem_type_sel }xms; my @matched; LINE51: while (my $line51 = <$ifh51>) { # no further processing unless something matches. next LINE51 unless $line51 =~ $rx_match; # remove rightmost comma. $line51 =~ s{ , (?= [^,]* \Z) }{}xms; # save what was matched and processed. push @matched, $line51; } dd \@matched; ^Z [ "bist_ctlr_bist_mode\n", "bist_ctlr_mem_type_sel\n", "bist_ctlr_mode_sel\n", ]