开发者

perl regex warning: \1 better written as $1 at (eval 1) line 1

use strict;
use warnings;

my $newPasswd = 'abc123';
my @lines = ( "pwd = abc", "pwd=abc", "password=def", "name= Mike" );

my %passwordMap = (
    'pwd(\\s*)=.*'      => 'pwd\\1= $newPasswd',
    'password(\\s*)=.*' => 'password\\1= $newPasswd',
);

print "@lines\n";

foreach my $li开发者_运维问答ne (@lines) {
    while ( my ( $key, $value ) = each(%passwordMap) ) {
        if ( $line =~ /$key/ ) {
            my $cmdStr = "\$line =~ s/$key/$value/";
            print "$cmdStr\n";
            eval($cmdStr);
            last;
        }
    }
}

print "@lines";

run it will give me the correct results:

pwd = abc pwd=abc password=def name= Mike
$line =~ s/pwd(\s*)=.*/pwd\1= $newPasswd/
\1 better written as $1 at (eval 2) line 1 (#1)
$line =~ s/password(\s*)=.*/password\1= $newPasswd/
\1 better written as $1 at (eval 3) line 1 (#1)
pwd = abc123 pwd=abc password= abc123 name= Mike

I don't want to see the warnings, tried to use $1 instead of \1, but it does not work. What should I do? Thanks a lot.


\1 is a regex pattern that means "match what was captured by the first set of capturing parens." It makes absolutely no sense to use that in a replacement expression. To get the string captured by the first set of capturing parens, use $1.

$line =~ s/pwd(\s*)=.*/pwd\1= $newPasswd/

should be

$line =~ s/pwd(\s*)=.*/pwd$1= $newPasswd/

so

'pwd(\\s*)=.*'      => 'pwd\\1= $newPasswd',
'password(\\s*)=.*' => 'password\\1= $newPasswd',

should be

'pwd(\\s*)=.*'      => 'pwd$1= $newPasswd',
'password(\\s*)=.*' => 'password$1= $newPasswd',

or better yet

qr/((?:pwd|password)\s*=).*/ => '$1= $newPasswd',


I see a lot of repetition in your code.

Assuming you're using Perl 5.10 or later, this is how I would have written your code.

use strict;
use warnings;
use 5.010;

my $new_pass = 'abc123';
my @lines = ( "pwd = abc", "pwd=abc", "password=def", "name= Mike" );

my @match = qw'pwd password';
my $match = '(?:'.join( '|', @match ).')';

say for @lines;
say '';

s/$match \s* = \K .* /$new_pass/x for @lines;
# which is essentially the same as:
# s/($match \s* =) .* /$1$new_pass/x for @lines;

say for @lines;


Assuming that the pattern of your pattern matching map stays the same, why not get rid of it and say simply:

$line =~ s/\s*=.*/=$newPassword/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜