【问题标题】:How to access and replace in perl? [duplicate]如何在 perl 中访问和替换? [复制]
【发布时间】: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


【解决方案1】:

使用替换运算符,有关此运算符的更多信息,请参阅::

http://perldoc.perl.org/functions/s.html

你可以通过简单的谷歌搜索在网上找到很多关于这个运算符的教程。

【讨论】:

  • 是的,我知道替换运算符.. 但在这种情况下 AT=2013/07/31-10:08:41 不会是恒定的.. 每次我 hv 获取值AT 和更换。我想知道如何获取!
【解决方案2】:

如果您将此作为字符串(可能来自配置文件),则可以:

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”替换为您的服务器时间。下次请先检查拼写。

【讨论】:

    【解决方案3】:

    这是使用在另一个变量上设置的时间(在本例中为 $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";
    

    【讨论】:

      猜你喜欢
      • 2021-04-05
      • 2014-10-17
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 2014-06-12
      • 2017-01-05
      • 1970-01-01
      • 2019-08-29
      相关资源
      最近更新 更多