Getting equivalent values using regular expression

Hi,

I have data (seperated by "\n") like this:

MN  - Regular expressions are a syntax, implemented in Perl

and certain other environments, making it not only possible but easy to do 

some of the following.

ID - 12,13

BA - Character classes are alternative single characters within square 

brackets, and are not to be confused with OOP classes, which are blueprints 

for objects.If not used carefully, they can yield unexpected results.

I want to match and retrieve the contents of MN and BA.

I tried like this:


if($str=~/MN\s+\-(.*)/)
{
   print "\n$1\n";
}
#This prints only part of the string (Regular expressions are a syntax, implemented in Perl)


If i try like this:

if($str=~/MN\s+\-(.*)\nID/)
{
   print "\n$1\n";
}
#Its not working!!!

The output should be like this:

Regular expressions are a syntax, implemented in Perl and certain other environments, making it not only possible but easy to do some of the following

Character classes are alternative single characters within square brackets, and are not to be confused with OOP classes, which are blueprints for objects.If not used carefully, they can yield unexpected results

what should i change in order to get that???

Regards
vanitha

Perl regex don't work across multiple lines (well, actually they do, if you use the m modifier to your regex, but even then only if all the lines are in the same variable). You have to first concatenate your lines by your own logic and then search for the stuff you want.

#!/usr/bin/perl -W

use strict;
use warnings;

my $line;
my %lines;
my $id;
while ( $line = <> ) {
    chomp $line;
    if ( $line =~ /^(\w{2})\s+-\s+/ ) {
        $id = $1;
        $line =~ /^\w{2}\s+-\s+(.*)/;
        $line = $1;
        $lines{$id} = "";
    }
    $lines{$id} .= $line . " ";
}
print $lines{"MN"}, "\n";
print $lines{"BA"}, "\n";