in reply to Unable to replace targeted newlines in a string

You have a string with a regular pattern of \n between the tokens. You want the first \n separated token and then the next 3 \n separated tokens. Maybe I have misunderstood the problem, but I would use split and slice for this job.

#!/usr/bin/perl -w use strict; my $string = "P5§2\nAAA\nBBB\nCCC\nP6§21\blah\nmore\n"; my ($first,@tokens) = (split(/\n/,$string))[0..3]; print "$first\n"; print @tokens,"\n"; __END__ Prints: P5§2 AAABBBCCC #Update: if you want the tokens in one $var, say $stuff, then #my $stuff = join("",@tokens); #print "$stuff\n"; # also prints: AAABBBCCC