【发布时间】:2013-07-30 15:41:57
【问题描述】:
我在一个变量中有当前服务器时间,我需要将AT=2013/07/31-10:08:41 替换为我的变量中存在的值。如何在 Perl 中替换它?
get(J=tesr,T=Bp,Act=A_Ti,AT=2013/07/31-10:08:41);
【问题讨论】:
标签: perl
我在一个变量中有当前服务器时间,我需要将AT=2013/07/31-10:08:41 替换为我的变量中存在的值。如何在 Perl 中替换它?
get(J=tesr,T=Bp,Act=A_Ti,AT=2013/07/31-10:08:41);
【问题讨论】:
标签: perl
【讨论】:
如果您将此作为字符串(可能来自配置文件),则可以:
use warnings;
use strict;
my $string = 'get(J=tesr,T=Bp,Act=A_Ti,AT=2013/07/31-10:08:41);';
$string =~ /AT=(.+)\);/;
my $new_time = 'new_time';
$string =~ s/$1/$new_time/;
print $string;
当然,您必须将“new_time”替换为您的服务器时间。下次请先检查拼写。
【讨论】:
这是使用在另一个变量上设置的时间(在本例中为 $new_time)替换 AT 值的一天
my $str = "get(J=tesr,T=Bp,Act=A_Ti,AT=2013/07/31-10:08:41);";
my $new_time = "2013/08/01-02:05:24";
$str =~ s/(AT=)(\d{4}\/(0[1-9]|1[0-2])\/([0-2][1-9]|3[0-1])-([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])/$1$new_time/g;
print "$str\n";
【讨论】: