#!/usr/local/bin/perl use strict; use warnings; ########################################################### #In this program there is a text always ontaining a title #followed by # #a sentence.I wish to check if there is a regexp B in the #actual sentence. # #If so, I wish to print the title of this sentence and the #sentence itself # #If the element B does not appear in the sentence, I want #both the sentence # #and its title to be #ignored. # ############################################################################# my $text = "A This is a title of the first sentence some text here containing the element B I need and more text here A This is a title of the second sentence C some text here but no element I need so it should be ignored A This is a title for the third sentence last sentence with the element B that I'm looking for"; my $expa = "A"; #title element which appears before every #sentence my $expb = "B"; #the regexp I'm loooking for my $line; # the line my $s; while($line = $text){ if($line =~m/$expa/){ #if A is found $s = $line; #then, read the following line $line = $text; if($line =~m/$expb/){ #if B is found print "$s\n\t$line\n\n\n"; #print both lines } } }