http://qs321.pair.com?node_id=683106


in reply to Multiple regex matches in single string

Given that your data file has thousands of lines you may be better choosing a different approach. I assume that end is always lower case. Set the input record separator to $/ = "\nend\n". Now each read of a file reads in a paragraph terminated by an end by itself on a line. The advantage is that you don't need to read in the whole file to memory and it should scale better.

local $/ = "\nend\n"; while ( my $data = <DATA> ) { $data =~ s/^.*start/start/is; print $data; print '=' x 10, "\n"; } __DATA__ start start start go one end start start start go two end
produces
start go one end ========== start go two end ==========

If my assumption of a lower case end is incorrect then you can achieve the same effect with

my $data = ''; while ( my $line = <DATA> ) { $data .= $line; if ( $line =~ /^end$/i ) { $data =~ s/^.*start/start/is; print $data; print '=' x 10, "\n"; $data = ''; } }