【问题标题】:Replacing or Substitution in Perl using Regex使用 Regex 在 Perl 中替换或替换
【发布时间】:2014-05-10 06:38:17
【问题描述】:

我不确定我在这里做错了什么。为什么正则表达式块不匹配并且没有发生替换。请帮忙。

#!usr/bin/perl

use strict;
use warnings;

my $x = << "END";

// @@@ START COPYRIGHT @@@
//
//        nth dimesion
//
//        Copyright 2007
//        nth dimension
//        Protected as an unpublished work.
//
//  The computer program listings, specifications and documentation 
//  herein are the property of nth dimension Company,
//  L.P., or a third party supplier and shall not be reproduced, 

END

$x=~s/\/\/\s+Copyright\s+\d{4}$/Copyright 2008/g;

print "$x\n";

打印 $x 打印相同的值。请帮忙。

【问题讨论】:

    标签: regex perl substitution string-substitution


    【解决方案1】:

    您需要/m 正则表达式开关,它将$ 视为行尾(而不是字符串结尾)

    $x=~s/\/\/\s+Copyright\s+\d{4}$/Copyright 2008/gm;
    

    如果您想保留号码中剩下的所有内容,您可以使用\K

    $x =~ s|//\s+Copyright\s+\K\d{4}$|2008|gm;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 2020-02-28
      • 2011-02-15
      • 1970-01-01
      • 2014-09-30
      • 1970-01-01
      相关资源
      最近更新 更多