0] # cat test_range while( $text= ){ print "$. $text"; if ( ($text=~/^AAAAAA/) .. ($text=~/^BBBBBB/ ) ){ if ($text=~/^AAAAA/){ print "========= Start of the fragment detected at $.\n"; } print "Still true at $.\n"; $end=$.; # Is this the only way to detect the end of the fragment? } } print "========= End of the fragment detected at $end.\n"; __DATA__ aaa aaa AAAAAA ccc ddd BBBBBB 111 222 333 CCCCCC 444 555 666 DDDDDD 777 888 999 #### [0] # perl test_range 1 aaa 2 aaa 3 AAAAAA ========= Start of the fragment detected at 3 Still true at 3 4 ccc Still true at 4 5 ddd Still true at 5 6 BBBBBB Still true at 6 7 111 8 222 9 333 10 CCCCCC 11 444 12 555 13 666 14 DDDDDD 15 777 16 888 17 999 ========= End of the fragment detected at 6.