http://www.perlmonks.org?node_id=11122313

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

Hi Monks,

I will be extremely thankful if the monks here could help me understanding regex in Perl.

Suppose I have a text file that has these lines:

This is line one. Line Two is this. Third line starts here. This is line four. This is line five. This is line six. This is the seventh line. This is line eight.

If the record contains the word "third" our "four", I want it to print the whole record, meaning the stuff between the empty lines, not just lines with those words in it. But I am not able to write it correctly. Here is what I have tried:

use strict; use warnings; open my $fh, "+<", "testlines.txt"; while (<$fh>) { if ($_=~ /(third | four)/si) { chomp; local $/ = "\n\n"; print "line is: $_\n"; } }

And it prints:

>perl regex.pl line is: Third line starts here. line is: This is line four.

But what I want is:

This is line one. Line Two is this. Third line starts here. This is line four

Where am I going wrong? Please guide. Please note, this is just an example. Depending on the data supplied the other lines in the file may contain some different words.

Not sure if this matters, but I'm on Windows 10 with Strawberry Perl version 5.32.