#!/usr/bin/perl local $/ = undef; my $string = ; $string =~ s/^(Heading Here:)$/\n$1/m; while ( $string =~ s/^(Heading Here:.*\n)(\S.+)(\nAnother Heading:)$/$1 $2$3/gsm ) {} while ( $string =~ s/^(Another Heading:.*\n)(\S.+)(\nEnd of section:)/$1 $2$3/gsm ) {} print $string; __DATA__ Key: Value Item: Another Item: Text Etc Etc: Whatever Heading Here: Indent This: Stuff By 2 spaces: Every line: Until The Next: Heading Efficiently: If possible Another Heading: More: Stuff: To: Indent End of section: Stop indenting Random: Test There: Is more stuff Further down: The string #### Key: Value Item: Another Item: Text Etc Etc: Whatever Heading Here: Indent This: Stuff By 2 spaces: Every line: Until The Next: Heading Efficiently: If possible Another Heading: More: Stuff: To: Indent End of section: Stop indenting Random: Test There: Is more stuff Further down: The string