in reply to Efficiently Extracting a Range of Lines (was: Range This)
All of the answers so far have not correctly replicated the behaviour of the original code - I don't know if this is really an issue but multiple START and END (sic! and not STOP - there's a typo in the original post) aren't matched. To do so I would suggest:
This captures into the array @good_stuff and can then be further processed. This is quite similar to the solution from tadman. Note the /m modifier to let ^ and $ match inside the string just before and after a newline. The /s modifier ensures that .*? matches everything including newlines.my @good_stuff = $stuff =~ /^START\n(.*?)^END$/gms; # or alternatively with START and END around my @good_stuff = $stuff =~ /(^START$.*?^END$)/gms;
Another issue are the benchmarks done here in this thread. These don't show anything, it depends on the structure of the real data.
as it has to backtrack from the end of the string.$stuff = << EOF; blah START a few lines END lots of lots of lines EOF
as the non-greedy .*? matching advances slower than greedy .*$stuff = << EOF; blah START lots of lots of lines END blah EOF
-- Hofmator
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(particle) Re: Re: Efficiently Extracting a Range of Lines (was: Range This)
by particle (Vicar) on Jul 11, 2001 at 19:27 UTC | |
by Hofmator (Curate) on Jul 11, 2001 at 19:45 UTC | |
|
Re: Re: Efficiently Extracting a Range of Lines (was: Range This)
by skazat (Chaplain) on Jul 11, 2001 at 20:39 UTC |