#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $file = 'textfile.txt'; open my $fh, '<', $file or die; $/ = undef; my $text = <$fh>; close $fh; my %res = $text =~ /BEGIN_TAG_(\d+)(.*?)END_TAG/sg; print Dumper (\%res);