in reply to Grouping strings over a few line

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.

Replies are listed 'Best First'.
Re^2: Grouping strings over a few line
by minixman (Beadle) on Oct 12, 2006 at 06:32 UTC
    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; }