我正在从事语言翻译项目,并被卡在中间的某处。修改和替换一个字符串中的引用子字符串
我有情况有像
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";
我不知道为什么替换失败。将不胜感激的帮助。
非常感谢。这工作完美。 我不解释'$你好'等等...为了将python代码翻译成perl,这是一个粗糙的中介解析步骤,我把'$'放在每个单词的前面,然后从关键字,字符串等中删除。 感谢您的帮助:) – Udeeksh 2014-09-27 06:13:16