#! /usr/bin/perl use warnings; use strict; my $text = << '__TEXT__'; SECTION 1 This is the first section. SECTION 2 And this is the second one. __TEXT__ my %section = ("", split /(SECTION \d+)/, $text); delete $section{""}; use Data::Dumper; print Dumper \%section;