简体   繁体   English

在Perl中替换2非默认行正则表达式

[英]Substitute 2 Non-default lines regular expression in Perl

I'd like to replace the reg expression of "hh:mm:ss" of 2 strings in Perl with "xx:xx:xx" How can I accomplish this? 我想用“ xx:xx:xx”替换Perl中2个字符串的“ hh:mm:ss”的正则表达式。

Code: 码:

use strict;
use warnings;
my $l="12:48:25 - Properties - submitMode : 2";
my $r="54:01:00 - Properties - submitMode : 2";
#my $newLn;
#Find "hh:mm:ss" in $_ :P
if ($l =~ /\d\d:\d\d:\d\d/ || $r=~ /\d\d:\d\d:\d\d/) {
#print "Time found";
s/\d\d:\d\d:\d\d/xx:xx:xx/g; #looking for default $_ , but have $l and $r
s/\d\d:\d\d:\d\d/xx:xx:xx/g;    
     #substitute with xx: p
print $l,"\n";
print $r,"\n";
} else {
print "No time found found";
}
$l =~ s/\d\d:\d\d:\d\d/xx:xx:xx/g;
$r =~ s/\d\d:\d\d:\d\d/xx:xx:xx/g;

toolic's solution works, but if you want to use the substitution command with the default variable $_ , use a foreach loop, like this: toolic的解决方案有效,但是如果您想将替换命令与默认变量$_使用,请使用foreach循环,如下所示:

use strict;
use warnings;
my $l="12:04:25 - Properties - submitMode : 2";
my $r="54:01:00 - Properties - submitMode : 2";
#my $newLn;
#Find "hh:mm:ss" in $_ :P
#if ($l =~ /\d\d:\d\d:\d\d/ || $r=~ /\d\d:\d\d:\d\d/) {

for ( $l, $r ) { 
    s/\d\d:\d\d:\d\d/xx:xx:xx/g || 
        do { 
            print "Not time found in $_\n"; 
            next 
        };
    print $_,"\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM