sumesh.sasi has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Can somebody tell me how to get the specific info from below
$_ = "%REPLYISSUED, Reply issued by ASI\blrssraj@EUR\Svap3930 :'(ACK) +Severity : - Serveur : Instance : PI1 date : 06/05/2008 time : 0 +0:01:04 status : MAXRUNALARM infomessage : RADD-METISS-FNT-PilotConf + errormessage Job still running. Alarmtime = 180 Mins errorcode 510 + returncode -656'";
I naeed to extract the blrssraj and message after (ACK). How do i do this with perl. When I tried with regex, in ASI\blr \b is taking as wordboundry

Replies are listed 'Best First'.
Re: regular expression issue
by moritz (Cardinal) on Jun 06, 2008 at 07:06 UTC
    You have to escape the backslash:
    m{ \|\\ # the delimiter ([^@]*) # everything but a @ @ .*? # garbage inbetween \(ACK\) (.*) # the rest }x; print "<$1>, <$2>\n";

    See regex tutorial and documentation for more details. (And please enclose your code and data examples with <code>...</code> tags.)

Re: regular expression issue
by pc88mxer (Vicar) on Jun 06, 2008 at 07:05 UTC
    Please use the <code>...</code> tags around perl code - it makes it a lot easier to read.

    Not knowing what kind of variation there could be in other messages of this type, I would take the following conservative approach to parsing it:

    if (m/Reply issued by (.*?):(.*)/) { my $issuer = $1; my $msg = $2; if ($issuer =~ m/([^\\]+)@/) { $issuer = $1; } $msg =~ s/\A\s*'\(ACK\)\s*//; $msg =~ s/\s*'\s*\z//; # results are in $issuer and $msg } else { # not a Reply issued by message }
    The idea is to match the issuer and message sections and then trim each appropriately. If there happen to be similar messages which don't conform exactly to this format, at least you'll get something back.

    Update: Changed \a in regex to \A - thanks moritz!

      Hello, I am not getting what I want. I extracted the out put from the first part ie.
      if (m/Reply issued by (.*?):(.*)/) { my $issuer = $1; my $msg = $2; print " $issuer \n $msg \n"; }
      the $issuer is as below, you can see the \b interpreted as regex \b. ASlrssrajSRVPARTSP3930 is the Out put
        In a double-quoted string, the sequence  "\b" is a backspace control code. See following examples in double- and single-quoted strings.

        perl -wMstrict -e "my $s = qq(abcdX\befg); print $s" abcdefg perl -wMstrict -e "my $s = qq(abcdX \befg); print $s" abcdXefg perl -wMstrict -e "my $s = q(abcdX\befg); print $s" abcdX\befg perl -wMstrict -e "my $s = q(abcdX \befg); print $s" abcdX \befg

        See Quote and Quote like Operators in perlop.