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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Grouping strings over a few line
by bart (Canon) on Oct 11, 2006 at 20:54 UTC
    Does grouping support multiple lines
    It does if everything is in one string, and you use the /s modifier, so /./ can match a "\n" too.

    If you read in the text one line at the time, you might reconsider using the .. or ... operator. That way, you can have a condition run over several lines in your data.

    For example:

    my $buffer; while(<>) { if(my $counter = (/^name\s.*\{/ ... /^name\s.*\{/)) { if($counter == 1) { # begin $buffer = ''; } $buffer .= $_; if($counter =~ /E/) { # end if($buffer =~ /name(.*)name/s) { print "Found: $1\n"; } redo; # retry with same line } } }

    Unfortunately this will only find one match in your test data, because there's only two "name" strings — thus, only one range between them.

      That looks great, but that only prints out the first name{} entry and not hte second, so it looks like it trys to create each name{} as a single string. Maybe that is the way to go is there a quick way to read the file and put it into a sinlge string ? for each name{] entry
        OK I experimented a little with eof, and I found eof (no handle, no parens) returns true on the last line. So If I extend the end side of the flipflop operator with an extra test on eof, and set a flag to true only if it succeeds because of this extra test, then I can safely check for the last match, too. I do need to modify the regex a little to allow for this, too.
        #! perl -w my $buffer; LINE: while(<DATA>) { my $eof; # flase by default if(my $counter = (/^name\s.*\{/ ... (/^name\s.*\{/ || ($eof = eof) +))) { if($counter == 1) { # begin $buffer = ''; } $buffer .= $_; if($counter =~ /E/) { # end if($buffer =~ /name(.*?)(^name|\z)/ms) { print "Found: $1\n"; } redo LINE unless $eof; # retry with same line, unless en +d condition is BECAUSE OF eof } } } __DATA__ name value { test 1; test 2; test 5; } name three { eat 4; eat 5; } name four { eat 6; eat 7; }
        Result:
        Found: value { test 1; test 2; test 5; } Found: three { eat 4; eat 5; } Found: four { eat 6; eat 7; }
Re: Grouping strings over a few line
by minixman (Beadle) on Oct 11, 2006 at 17:59 UTC