use strict; my $string ='hfkjd process jhk start begin output1 output2 end process scrap hkl process khfk shoot hooter begin output3 end process'; while($string=~/\bbegin((?:(?!end process).)*)end process/sg){ print "IN:$1\n"; }