【发布时间】:2018-07-03 18:42:12
【问题描述】:
我想在一行(字符串)中搜索和替换多个字符串。
考虑我有三个变量
my $fruitone = "apple";
my $fruittwo = "orange";
my $fruitthree = "banana";
my string1 = "I have one ${fruitone} two ${fruittwo} and three ${fruitthree}";
我想用apple替换$fruitone等等。
我的最终结果应该是这样的
I have one apple two orange and three banana.
我可以用string1 =~ /$\{(\w+)\}/$$1/;替换一个
但我在访问 $2 和 $3 项目方面需要帮助
【问题讨论】:
-
my string是语法错误(缺少 $)。此外,双引号会插入变量。 -
我相信我没有理解这个问题。 :-( 当 Perl 会自动执行时,“手动”在字符串中插入变量的目的是什么?
"I have one ${fruitone} …"无论如何都会产生"I have one apple …"(使用"时),不是吗?