in reply to Store multiple lines

If it is safe to assume that the "service-name" line immediately follows the "static-route" line, and the file is not huge, then I might read all of the data into a scalar then remove the \nxxxxxxxx- parts globally so that all the information for each item was on one line. That would remove the need for the logic of two separate regex matches simplifying the data extraction. I would then open a reference to the scalar and read that line by line. Something like:-

use strict; use warnings; open my $inFH, q{<}, \ <<EOD or die qq{open: < \ HEREDOC: $!\n}; dnsname1: static-route next-hop preferen +ce 200 bfd-enable tag 685 dnsname1- service-name "XYZ12345-s12345" dnsname2: static-route next-hop bfd-enab +le tag 635 dnsname2- service-name "XYZ67891-s67890" EOD my $data = do { local $/; <$inFH>; }; close $inFH or die qq{close: < \ HEREDOC: $!\n}; $data =~ s{\n\S+-}{}g; open $inFH, q{<}, \ $data or die qq{open: < \ scalar: $!\n}; while (<$inFH> ) { # Do a single data extraction regex here. ... } close $inFH or die qq{close: < \ scalar: $!\n};

I hope this is helpful.