1

Perl の 2 つの文字列の "hh:mm:ss" の正規表現を "xx:xx:xx" に置き換えたいのですが、どうすればこれを達成できますか?

コード:

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";
}
4

2 に答える 2

2

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";
}
于 2012-07-05T20:54:21.687 に答える
2
$l =~ s/\d\d:\d\d:\d\d/xx:xx:xx/g;
$r =~ s/\d\d:\d\d:\d\d/xx:xx:xx/g;
于 2012-07-05T20:48:56.243 に答える