Unknown escape \ m went through regex error with PERL

I am trying to change a row using Regex and a hash table containing the changes. I use the following code to change:

    foreach $key (keys %{$hash{$sub_hash}}){
        $line =~ s/$key/$hash{$csub_hash}{$key}/g;
    }

And my hash:

    $hash{sub_hush_a}={
    "\\mi2ie..."    =>  "\\RSUop...",
    "iereset..."    =>  "\\agres...",
};

The problem is that for the first pair in the data set, when it ever changes, it \is placed instead \\, and for the second - only one, as expected.

It also gives me an error: Unrecognized escape \m passed through. What's wrong?

Explanation: There are no dots in the names in the program, names are longer than me, and they all contain only letters and numbers. points here to cut.

EDIT:

The problem resolves (double \and error message) if I change the fist pair:

"mi2ie..." => "RSUop...",(delete \\).

, , , .

+3
3

, , Regex. / . Perl Dirty Dozen:

\ |() [{^ $* +?.

(\Q..\E ):

foreach $key (keys %{$hash{$sub_hash}}){
    $line =~ s/\Q$key\E/$hash{$csub_hash}{$key}/g;
}
+5

, , , , .

, , qr (http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators).

0
my $line = 'test\mi2ie...test';

sub replace($$$) {
   my ($str, $what, $replacement) = @_;
   my $pattern = quotemeta($what);
   $str =~ s/$pattern/$replacement/g;
   return $str;
}

my %hash;
my $sub_hash = "test";
$hash{$sub_hash} = {
    '\mi2ie...'    =>  '\RSUop...',
    'iereset...'    =>  '\agres...',
};

while (my ($key, $value) = each %{$hash{$sub_hash}}) {
    $line = replace($line, $key, $value);
}

print "$line\n";
0
source

Source: https://habr.com/ru/post/1780662/


All Articles