$s = "head\nstuff\nstuff\ntail\nthis\nthat\nhead\nother stuff\nmore\ntail\nand more";; while( $s =~ m[head\n(.+?)tail\n]smg ) { print ">$1<"; };; >stuff stuff < >other stuff more <