Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex perl替换字符串中的时间戳数字_Regex_Perl - Fatal编程技术网

Regex perl替换字符串中的时间戳数字

Regex perl替换字符串中的时间戳数字,regex,perl,Regex,Perl,我在使用perl动态替换时间戳时遇到了一个问题。在config.pm中,我有一个时间戳的占位符: our $PrintableTimeStamp = "20130101_010101"; 我想用动态生成的当前时间戳替换数字,即 $current = $year.$mon.$mday."_".hour.$min.$sec; # (e.g., 20131230_153001) 我使用了以下命令工作: perl -p -i.bak -e s/20130101_010101/$current/g c

我在使用perl动态替换时间戳时遇到了一个问题。在config.pm中,我有一个时间戳的占位符:

our $PrintableTimeStamp = "20130101_010101";
我想用动态生成的当前时间戳替换数字,即

$current = $year.$mon.$mday."_".hour.$min.$sec; # (e.g., 20131230_153001)
我使用了以下命令工作:

perl -p -i.bak -e s/20130101_010101/$current/g config.pm
但不是下面这个,我希望它能更通用、更灵活

perl -p -i.bak -e s/\d{8}_\d{6}/$current/g config.pm

任何原因?

你应该考虑绑定<代码> = ~> /Cord>,你可能需要将<代码>()>代码>你的正则表达式中的数字串。下面是我编写的一个例程示例,用于在数据库查询中使用MAC地址之前验证其长度是否正确:

sub verify { #{{{ pull out the colons, verify char = 12, replace colons
my @newmac;
    foreach my $hostmac (@_) {
    chomp($hostmac);
    if ($hostmac =~ /(?:[A-F0-9]{2}:){5}[A-F0-9]{2}/) {
        push (@newmac,$hostmac);
    } else {
        my $count;
        $hostmac =~ s/\://g; # take out the colons
        if ($hostmac =~ /^.{12}$/ ) { # make sure we have a 12 character string
            $hostmac = sprintf "%s:%s:%s:%s:%s:%s", unpack("(A2)6","\U$hostmac\E");
            push (@newmac, $hostmac);
        } else {
            print "$hostmac\n";
            print colored ("$hostmac should be 12 characters long\n", 'red'); die; # You FAIL!!
        }
    }
}
return $newmac[0] if $#newmac == 1;
return @newmac;

}

什么设置<代码>$当前?起初,我认为它是一个shell变量,但您展示了它是使用Perl代码构造的。这里有些不对劲。你能更准确地描述一下这个问题吗?我已经解决了我的问题。应该是perl-p-i.bak-e s/\\d{8}\\\d{6}/$current/g config.pm。还发现,如果在system()调用下调用此特定正则表达式语法,则该语法可能无法正常工作,必须使用eval{run\@command…}使其正常工作。非常棘手。@user3147658您真的没有在命令行代码中使用引号吗?也就是说,
perl-e'…'(注意单引号)。