在Perl中,使用正则表达式对字符串执行替换并将值存储在不同的变量中而不改变原始值的好方法是什么?

我通常只是复制字符串到一个新变量,然后将其绑定到s///正则表达式,它对新字符串进行替换,但我想知道是否有更好的方法来做到这一点?

$newstring = $oldstring;
$newstring =~ s/foo/bar/g;

当前回答

我讨厌foo和bar ..到底是谁在编程中想出了这些非描述性的术语?

my $oldstring = "replace donotreplace replace donotreplace replace donotreplace";

my $newstring = $oldstring;
$newstring =~ s/replace/newword/g; # inplace replacement

print $newstring;
%: newword donotreplace newword donotreplace newword donotreplace

其他回答

在使用严格的情况下,说:

(my $new = $original) =~ s/foo/bar/;

代替。

5.14之前的另一个解决方案:http://www.perlmonks.org/?node_id=346719(参见japhy的帖子)

由于他的方法使用map,它也适用于数组,但需要级联map来产生一个临时数组(否则原始数组将被修改):

my @orig = ('this', 'this sucks', 'what is this?');
my @list = map { s/this/that/; $_ } map { $_ } @orig;
# @orig unmodified

这是我一直用来在不改变原始字符串的情况下获得修改后的字符串副本的习惯用法:

(my $newstring = $oldstring) =~ s/foo/bar/g;

在perl 5.14.0或更高版本中,你可以使用新的/r非破坏性替换修饰符:

my $newstring = $oldstring =~ s/foo/bar/gr; 

注意: 上述解在没有g的情况下也适用。它们也适用于任何其他修饰符。

参见: perldoc: Perl正则表达式快速入门

如果我只是在lineer中使用这个,怎么样,sprintf(“%s”,$oldstring)

一行程序解决方案作为一种陈词滥调比好的代码更有用;好的Perl程序员会知道并理解它,但是它比您开始使用的两行复制-修改对联更不透明和可读。

换句话说,这样做的好方法是你已经在做的方法。以可读性为代价的不必要的简洁并不是一种胜利。