#!/usr/bin/perl use strict; use warnings; my %res; { # for localizing $/ local $/ = 'END_TAG'; /BEGIN_TAG_(\d+)(.*)END_TAG/s and $res{$1} = $2 while ; } use Data::Dump 'dd'; dd \%res; __DATA__ Any text is in this document. But I'm only interested in the text between a beginning and an end tag. BEGIN_TAG_110 In this text I'm interested in. It can consist of several lines. Until the end tag. END_TAG and any other things I'm not interested in until the next beginning tag starts. Any other text. BEGIN_TAG_237 I need this text. END_TAG More text ... #### { 110 => "\nIn this text I'm interested in.\nIt can consist of several lines. Until the end tag.\n", 237 => "\nI need this text.\n", }