在Perl中,使用正则表达式对字符串执行替换并将值存储在不同的变量中而不改变原始值的好方法是什么?
我通常只是复制字符串到一个新变量,然后将其绑定到s///正则表达式,它对新字符串进行替换,但我想知道是否有更好的方法来做到这一点?
$newstring = $oldstring;
$newstring =~ s/foo/bar/g;
在Perl中,使用正则表达式对字符串执行替换并将值存储在不同的变量中而不改变原始值的好方法是什么?
我通常只是复制字符串到一个新变量,然后将其绑定到s///正则表达式,它对新字符串进行替换,但我想知道是否有更好的方法来做到这一点?
$newstring = $oldstring;
$newstring =~ s/foo/bar/g;
当前回答
如果我只是在lineer中使用这个,怎么样,sprintf(“%s”,$oldstring)
其他回答
如果我只是在lineer中使用这个,怎么样,sprintf(“%s”,$oldstring)
在使用严格的情况下,说:
(my $new = $original) =~ s/foo/bar/;
代替。
如果使用strict;来编写Perl,那么您会发现一行语法是无效的,即使声明了也是如此。
:
my ($newstring = $oldstring) =~ s/foo/bar/;
你会得到:
Can't declare scalar assignment in "my" at script.pl line 7, near ") =~"
Execution of script.pl aborted due to compilation errors.
相反,您一直在使用的语法,虽然多了一行,但使用strict;在语法上是正确的。对我来说,使用使用严格;现在只是一种习惯。我是自动做的。每个人都应该。
#!/usr/bin/env perl -wT
use strict;
my $oldstring = "foo one foo two foo three";
my $newstring = $oldstring;
$newstring =~ s/foo/bar/g;
print "$oldstring","\n";
print "$newstring","\n";
一行程序解决方案作为一种陈词滥调比好的代码更有用;好的Perl程序员会知道并理解它,但是它比您开始使用的两行复制-修改对联更不透明和可读。
换句话说,这样做的好方法是你已经在做的方法。以可读性为代价的不必要的简洁并不是一种胜利。
声明:
(my $newstring = $oldstring) =~ s/foo/bar/g;
这相当于:
my $newstring = $oldstring;
$newstring =~ s/foo/bar/g;
或者,从Perl 5.13.2开始,你可以使用/r来做一个非破坏性的替换:
use 5.013;
#...
my $newstring = $oldstring =~ s/foo/bar/gr;