benchrifa has asked for the wisdom of the Perl Monks concerning the following question:

Hello everybody,

This is my code:

#!/usr/bin/perl my $string = "P5§2\nAAA\nBBB\nCCC\nP6§21\n"; $string =~ s/^(P\d+[^\n]*?\n) (([^\n]+) (\n))+ (?:P\d+[^\n]*?)$ /$1$2/xsmig;# print "\$string is \n$string \n";

I got this output:

P5º2 CCC

And I would like to get this one:

P5º2 AAABBBCCC

Thank you for your help

Benchrifa

Replies are listed 'Best First'.
Re: Unable to replace targeted newlines in a string
by moritz (Cardinal) on Oct 30, 2009 at 11:20 UTC
    A quantified capturing group (...)+ only stores the last match in the dollar-number variable it is associated with.

    So I recommend capturing all the AAA to CCC lines in a single capture, and remove the newlines from that in a separate step. Or even simple, split on /^(?=P\d)/m and then remove newlines from the chunks.

    Perl 6 - links to (nearly) everything that is Perl 6.

      Thank you for your explanation: crystal clear.

      Benchrifa

Re: Unable to replace targeted newlines in a string
by Marshall (Canon) on Oct 31, 2009 at 11:47 UTC
    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