toadi has asked for the
wisdom of the Perl Monks concerning the following question:

Hi,

I program perl for a while now and still suck at regexes.

My file looks like:

STARTP
TITLE
some gibberish
some more gibberish
ENDTITLE
TITLE
some gibberish
some more gibberish
ENDTITLE
TITLE
some gibberish
some more gibberish
ENDTITLE
ENDP
STARTP
TITLE
some gibberish
some more gibberish
ENDTITLE
TITLE
some gibberish
some more gibberish
ENDTITLE
TITLE
some gibberish
some more gibberish
ENDTITLE
ENDP

I want to get everyting from STARTP until ENDP. En then cut stuff up between TITLE and ENDTITLE. But if I do like the suggested link I get everything fron first STARTP until last ENNDP. And I want to match first from First STARTP until first ENDP in the file and then from next STARTP until next ENDP. And the same for TITLE and ENDTITLE.

And no there is no recursion in these tags.

thanx

--My opinions may have changed,
but not the fact that I am right

janitored by ybiC: Retitle from one-word "regex" nodetitle to avoid hindering site searching. Also converted node link from <a href...> to Monastery style [id://nnnn] to avoid logging out monks with cookie set from different PM domain (perlmonks.(org|net), sans leading "www"...)

#! /usr/bin/perl -w
use strict;
my @titles;
while (<DATA>)
{
if (my $num = /TITLE/ .. /ENDTITLE/)
{
push @titles, $_ unless $num == 1 || $num =~ /E/;
}
}
print for @titles;
__DATA__
STARTP
TITLE
some gibberish
some more gibberish
ENDTITLE
TITLE
some gibberish
some more gibberish
ENDTITLE
TITLE
some gibberish
some more gibberish
ENDTITLE
ENDP
STARTP
TITLE
some gibberish
some more gibberish
ENDTITLE
TITLE
some gibberish
some more gibberish
ENDTITLE
TITLE
some gibberish
some more gibberish
ENDTITLE
ENDP

Although I like a spicy regex as much as the next guy, I think that the use of the flip-flop operator, as pointed out by pbeckingham and diebyz is much more elegant and (probably) more efficient, since it allows you to avoid slurping the file that is being parsed.

The 'scalar range' / 'flip-flop' operator is one of the sweetest pieces of syntactical sugar that Perl offers, if you ask me.

Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"