in reply to Regular Expressions Challenge

Why use a complicated regular expression?
my @section; my $i = -1; foreach (split /\n/, $str) { if (/^===Comments===$/) { ++$i; next; } next unless $i >= 0; $section[$i] .= "$_\n"; }
Now @section will contain all the text after ===Comments===, one chapter at a time.

Replies are listed 'Best First'.
Re^2: Regular Expressions Challenge
by danj35 (Sexton) on May 18, 2010 at 07:41 UTC
    This code is all well and good, except it takes the information after the end of each comments section also. I need to take only the information within each comments section (e.g. between ===Comments=== and =Microarray Data=). Sorry if that wasn't clear.
      my @data = split /\n/, $str; my @section; my $i = 0; while (@data) { $_ = shift @data; if (/^===Comments===$/) { while (@data) { $_ = shift @data; last if /^=/; $section[$i] .= "$_\n"; } $i++; } }