Hye,你能检查一下我的脚本吗我的problem..sorry是新的..我想把数字转换成英文单词例如1400 -> 1,400...我已经用过了
Lingua::EN::Numbers qw(num2en num2en_ordinal);
这是我的输入file.txt
I have us dollar 1200 输出应该是。“我有1,200美元”
这是我的脚本
#!/usr/bin/perl
use utf8;
use Lingua::EN::Numbers qw(num2en num2en_ordinal);
if(! open(INPUT, '< snuker.txt'))
{
die "cannot opent input file: $!";
}
select OUTPUT;
while($lines = <INPUT>){
$lines =~ s/usd|USD|Usd|uSd|UsD/us dollar/g;
$lines =~ s/\$/dollar /g;
$lines =~ s/rm|RM|Rm|rM/ringgit malaysia /g;
$lines =~ s/\n/ /g;
$lines =~ s/[[:punct:]]//g;
$lines =~ s/(\d+)/num2en($lines)/g; #this is where it should convert to english words
print lc($lines); #print lower case
}
close INPUT;
close OUTPUT;
close STDOUT;我得到的输出是"i have us dollar num2en(i have us dollar 1200 )“
谢谢
发布于 2013-03-26 16:07:04
您需要使用$1引用捕获,而不是在最后一个正则表达式中传递$lines,在最后一个正则表达式的末尾还需要一个e标志,以便将其作为表达式进行计算。您可以使用i标志来避免写入UuDd...的所有组合:
while($lines = <INPUT>){
$lines =~ s/usd/us dollar/ig;
$lines =~ s/\$/dollar /g;
$lines =~ s/rm/ringgit malaysia /ig;
$lines =~ s/\n/ /g;
$lines =~ s/[[:punct:]]//g;
$lines =~ s/(\d+)/num2en($1)/ge; #this is where it should convert to english words
print lc($lines), "\n"; #print lower case
}发布于 2013-03-26 16:03:10
正则表达式替换中缺少e修饰符:
$ echo foo 42 | perl -pe "s/(\d+)/\$1+1/g"
foo 42+1
$ echo foo 42 | perl -pe "s/(\d+)/\$1+1/ge"
foo 43请参阅man perlop
选项与m//相同,但增加了以下替换特定选项:
它会把右边作为一种表达来评估。
此外,您必须引用捕获的数字($1),而不是整个字符串($lines),但我猜您已经捕获到了这一点。
发布于 2013-03-26 16:02:54
这里的问题是您混淆了regexp和函数。在您尝试执行转换的行中,您不会调用函数num2en;相反,您将用文本num2en($line)替换数字。给你一个建议:
($text, $number) = $lines =~ s/(.*)+(\d+); # split the line into a text part and a number part
print lc($text . num2en($number)); # print first the text, then the converted number;https://stackoverflow.com/questions/15631988
复制相似问题