bem*_*m33 5 regex variables perl modifier substitution
有没有办法在替换中使用变量作为修饰符?
my $search = 'looking';
my $replace = '"find: $1 ="';
my $modifier = 'ee';
s/$search/$replace/$modifier;
Run Code Online (Sandbox Code Playgroud)
我需要使用哈希数组来使用不同的修饰符进行批量搜索替换.
小智 3
嗯,如果我必须这样做,我会这样做:
use warnings;
use strict;
my @stuff = (
{
search => "this",
replace => "that",
modifier => "g",
},
{
search => "ono",
replace => "wendy",
modifier => "i",
}
);
$_ = "this ono boo this\n";
for my $h (@stuff) {
if ($h->{modifier} eq 'g') {
s/$h->{search}/$h->{replace}/g;
} elsif ($h->{modifier} eq 'i') {
s/$h->{search}/$h->{replace}/i;
}
# etc.
}
print;
Run Code Online (Sandbox Code Playgroud)
您可能想要使用的不同修饰符只有这么多,所以我认为这很简单。
你可以用eval
这个,但它非常混乱。
归档时间: |
|
查看次数: |
1701 次 |
最近记录: |