in reply to Re: Search all occurences of text delimited by START and END in a string
in thread Search all occurences of text delimited by START and END in a string
If they are concerned about holding the whole file in memory, there is a convenient choice for record separator:#!/usr/bin/perl use strict; use warnings; use diagnostics; my $start = '<START>'; my $end = '<END>'; my $data = do { local $/; <DATA>; }; while ($data =~ /\Q$start\E(.+?)\Q$end\E/sg) { print "$1\n"; } __DATA__ <START>TEXT1<END> <various data> <START>TEXT2<END> <various data> <START>TEXT3<END>
where I've kept the regex as is since the last record will not be <END> delimited, and so there'd be a failure for an unmatched <START>#!/usr/bin/perl use strict; use warnings; use diagnostics; my $start = '<START>'; my $end = '<END>'; local $/ = $end; while (<DATA>) { while (/\Q$start\E(.+?)\Q$end\E/sg) { print "$1\n"; } } __DATA__ <START>TEXT1<END> <various data> <START>TEXT2<END> <various data> <START>TEXT3<END>
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|