in reply to RegExp Loop
I'm trying to keep your logic mostly the same, though I'm not sure I entirely understand it. If you're reading from a file, substitute the filehandle for DATA (or if you want the default, you can just use <>.#!/usr/bin/perl use strict; use warnings; my $expa = "A"; #title element which appears before every #sentence my $expb = "B"; #the regexp I'm loooking for my $s; while (<DATA>){ chomp; if (/$expa/){ $s = $_; chomp($_ = <DATA>); if (/$expb/){ print "$s\n\t$_\n\n\n"; } } } __DATA__ 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
|
|---|