use strict; use warnings; my $offset = 0; # Where are we in the string? my $string = do { # Grab the string. local $/; ; }; my $numResults = 0; while (1) { my $idxSummary = index($string, "SUMMARY", $offset); my $result = ""; if ($idxSummary > -1) { $offset = $idxSummary + length("SUMMARY"); my $idxDescription = index($string, "DESCRIPTION", $offset); if ($idxDescription == -1) { print "(Data malformed: missing DESCRIPTION line.)\n"; last; } my $length = $idxDescription - $offset; $result = substr($string, $offset, $length); $offset = $idxDescription + length("DESCRIPTION"); $result =~ s/^\s+|\s+$//g ; # Strip leading and trailing whitespace, # includng newlines. $numResults++; } else { print "(All done. $numResults result(s) found.)\n"; last; } print " <$result>\n"; } __DATA__ This is bogus data SUMMARY Event 1 DESCRIPTION Lorem ipsum etc etc This is bogus data SUMMARY Event 2 DESCRIPTION Lorem ipsum This is bogus data SUMMARY The Third Event DESCRIPTION Lorem ipsum This is bogus data SUMMARY Event Number Four DESCRIPTION Lorem ipsum