如何在替换中使用变量作为修饰符 [英] How to use a variable as modifier in a substitution
本文介绍了如何在替换中使用变量作为修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在替换中使用变量作为修饰符?
Is there a way to use a variable as modifier in a substitution?
my $search = 'looking';
my $replace = '"find: $1 ="';
my $modifier = 'ee';
s/$search/$replace/$modifier;
我需要使用哈希数组来使用不同的修饰符进行批量搜索替换.
I need to use an array of hashes to make bulk search-replace with different modifiers.
推荐答案
嗯,如果必须这样做,我会这样做:
Hm, if I had to do it I would do like this:
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;
您可能只想使用许多不同的修饰符,所以我认为这很容易.
There are only so many different modifiers you might want to use so I think this is easy enough.
您可以使用eval
来完成此操作,但是它非常混乱.
You can use eval
for this, but it's awfully messy.
这篇关于如何在替换中使用变量作为修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文