【发布时间】:2014-11-22 03:50:37
【问题描述】:
我正在做一个语言翻译项目,但被困在中间的某个地方。
我遇到类似字符串的情况
print "$Hi $There","$Welcome $Aboard"
我想得到
print "Hi There", "Welcome Aboard"
即提取带引号的子字符串,去掉'$'并用新的子字符串替换原来的。
我能够提取和更改引用的子字符串,但是当我尝试将它们替换为原始子字符串时,它不起作用。向您展示示例代码:
#!/usr/bin/perl
use strict;
use warnings;
my $str = "print \"\$Hi \$There\",\"\$Welcome \$Aboard\"";
print "Before:\n$str\n";
my @quoted = $str =~ m/(\".*?\")/g; #Extract all the quoted strings
foreach my $subStr (@quoted)
{
my $newSubStr = $subStr;
$newSubStr =~ s/\$//g; #Remove all the '$'
$str =~ s/$subStr/$newSubStr/g; #Replace the string**::Doesn't work**
}
print "After:\n$str\n";
我不知道替换失败的原因。非常感谢您的帮助。
【问题讨论】: