in reply to multiple line regex

If you can't slurp the whole file then you could do something like this.

#! perl -slw use strict; my $buffer; while( <DATA> ) { $buffer .= $_; # Accumulate lines in a buffer # Use \s+ instead of space to allow for newlines between words # The /g option is required to set pos() if( $buffer =~ /Content\s+Compression\s+to\s+Date/g ) { print "Content Compression to Date found"; # Once you find something your looking for # throw away everything that preceded it with substr() substr( $buffer, 0, pos( $buffer ) ) =''; } } __DATA__ Content Compression to Date other stuff other stuff other stuff Content Compression to Date other stuff other stuff other stuff Content Compression to Date Content Compression to Date other stuff other stuff Content Compression to Date other stuff other stuff

Output

P:\test>test2 Content Compression to Date found Content Compression to Date found Content Compression to Date found Content Compression to Date found Content Compression to Date found

Notes:

You will need to use the /g option on ALL the regexes, even though you are only looking for the first match each time in order for pos to be set. (I never quite understood that?).

You will also need to do the substr thing to throw away everything already matched after each successful match.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Replies are listed 'Best First'.
Re: Re: multiple line regex
by js1 (Monk) on Apr 23, 2004 at 09:52 UTC

    Thanks for your replies. Both of them are very useful.

    js1.