my $string = 'stuff' # This should contain your original text. s/\n([a-zA-Z]+)\n/\n\\chapter{$1}\n/g;