use strict; use warnings; local $/='end'; my @aFound; while (my $line=) { # normally we would chomp to get rid of 'end' # but this may not be a good idea if the file ends in # junk outside of start ... end. # chomp $line; # make sure we really have a match just in case there is # an 'end' without a preceding "start"! # also use the s modifier at the end of your regex so that # . matches "\n" # see http://perldoc.perl.org/perlre.html#Modifiers # for further information next unless ($line =~ /\s+start\s+(.*)end\z/s); # store extracted string for later use push @aFound, $1; } # do something with the text between start...end # you'll want to change this: # your post looks like you would like to further separate # each word on a separate line, but for now, lets just # print out the run of characters between start...end. print join("\n", @aFound), "\n"; __DATA__ asdasd start asdasd asdasdasd asdasdas end asdasdas adasdas start as asdas dasdasdad asdasddas end qweqwe asdasd start asdsadsdasddasds sdasdas asdasdasdasd asdasdsa asdasd asdasdasd end here is some trailing garbage #### asdasd asdasdasd asdasdas as asdas dasdasdad asdasddas asdsadsdasddasds sdasdas asdasdasdasd asdasdsa asdasd asdasdasd