in reply to Re: Regular Expressions Challenge
in thread Regular Expressions Challenge
Not sure if this is neater or simpler, but certainly shorter - maybe you would like one of the following:
my @sections = $str =~ m/^===Comments===\n(.*?)\n=.*?=$/gms; print Dumper(\@sections); print "\n\nOR\n\n"; my %sections = reverse( $str =~ m/^===Comments===\n(.*?)\n=(.*?)=$/gms + ); print Dumper(\%sections); print "\n\nOR\n\n"; while($str =~ m/^===Comments===\n(.*?)\n=(.*?)=$/gms) { print " $2 : $1\n"; }
Which produces:
$VAR1 = [ ' User comments are added here. A user may write whatever they may wish. ', ' Comments are related to the microarray data here. ', ' Comments related to the pathway information here. ' ]; OR $VAR1 = { 'Microarray Data' => ' User comments are added here. A user may write whatever they may wish. ', 'Pathway Information' => ' Comments are related to the microarray data here. ', 'Aditional Info' => ' Comments related to the pathway information here. ' }; OR Microarray Data : User comments are added here. A user may write whatever they may wish. Pathway Information : Comments are related to the microarray data here. Aditional Info : Comments related to the pathway information here.
update: but I see the last two are wrong, now that I've posted it.
my %x = map { /^=([^\n]*)=\n.*\n===Comments===\n(.*)$/s ? ( $1 => $2 ) : () } split(/(?=^=[^=])/m,$str); print Dumper(\%x);
gives
$VAR1 = { 'Microarray Data' => ' Comments are related to the microarray data here. ', 'Pathway Information' => ' Comments related to the pathway information here. ', 'Literature' => ' User comments are added here. A user may write whatever they may wish. ' };
|
|---|