chirag
chirag

Reputation: 71

Why below code is not working as expected?

I have below code to match particular keyword from file, Please note that particular keyword is present in that file. (Verified)

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;

my $fname="sample.txt";
my @o_msg_rx;

my $tempStr='=?UTF-8?B?U2Now4PCtm5l?=\, Ma ';

push @o_msg_rx, $tempStr;

foreach my $rx_temp (@o_msg_rx) {
        print "rx_temp = $rx_temp\n";
}

my @msg_arr;
open MM, '<', $fname;
chomp(@msg_arr = (<MM>));
close MM;

my (%o_msg_rx, %msg_anti_rx);
foreach my $rx (@o_msg_rx){
        ($rx =~ s/^!// ? $msg_anti_rx{$rx} : $o_msg_rx{$rx}) = 0 if $rx;
        print "rx = \t$rx\n";
        print "o_msg_rx_rx = \t$o_msg_rx{$rx}\n";
}
if(@msg_arr) {
        foreach my $rx (keys %o_msg_rx) {
                $o_msg_rx{$rx} = 1 if grep (/$rx/i, @msg_arr);
        }
}

my $regex_ok = (! scalar grep (! $o_msg_rx{$_}, keys %o_msg_rx));

print "regex_ok = $regex_ok\n";

I am attaching few lines from the file for clarification.

# Step: 23    14:48:52
#  
# var: expect-count='1'
# var: msg-rx=""=?UTF-8?B?U2Now4PCtm5l?=\, Maik ""
# etc etc etc

Upvotes: 0

Views: 63

Answers (1)

user2404501
user2404501

Reputation:

Do you intend for $tempStr to be interpreted as a regular expression? If so, then you should know that the ? is a regular expression operator and will not literally match a ? in the target string.

Also, it has a space after Ma but your sample file has Maik so that part won't match.

These changes will produce a different result:

my $tempStr='=?UTF-8?B?U2Now4PCtm5l?=\, Ma'; # remove the extra space

grep (/\Q$rx/i, @msg_arr); # Add \Q to match the literal string $tempStr in regexp

Or you could make $tempStr a real regexp from the start:

my $tempStr=qr/=\?UTF-8\?B\?U2Now4PCtm5l\?=\\, Ma/;

Or you could leave it as a string but put it in regexp syntax (needs an extra doubling of the double backslash, very ugly):

my $tempStr='=\?UTF-8\?B\?U2Now4PCtm5l\?=\\\\, Ma';

Upvotes: 2

Related Questions