2009-07-06 75 views
-2

我试图用£替换HTML文件中的所有£符号。我的正则表达式似乎不起作用。如何用Perl替换£中的所有文件?

你能帮忙吗?

+1

显示你尝试过什么,没有工作,使人们可以看到什么地方出了错,并帮助你? – ysth 2009-07-06 08:37:39

+0

你一直在评论“问题是什么”,但没有人会告诉你,因为你没有说你做了什么。你想在Vim做这个吗?如果是这样,_how_?你想用Perl来做这个吗?如果是这样,_how_? – Telemachus 2009-07-06 10:34:44

+0

对不起,它之前,我插入其他评论并删除它。现在问题得到解决 – joe 2009-07-06 10:37:08

回答

2

这应该工作,

#!/usr/bin/perl 
# File: convert.pl 
no utf8; # its not required 
while (<>) { 
    s/(\xa3)/pound/g; 
     print; 
} 

因为£在我的hexdump上显示为0xA3

但是,这样会

#!/usr/bin/perl 
while (<>) { 
    s/£/pound/g; 
     print; 
} 

只是说

chmod a+x convert.pl 
convert.pl yourfile.html > newfile.html 
4

你很可能忘了:

use utf8; 

试试下面的程序:

#!/usr/bin/perl 

use strict; 
use warnings; 
use utf8; 

while (<DATA>) { 
    s/£/&pound;/g; 
    print 
} 

__END__ 
This is sample text with lots of £££! 
50£ is better than 0£. 

如果你想从一个名为input文件的读取和写入名为output文件:

#!/usr/bin/perl 

use strict; 
use warnings; 
use utf8; 

open my $input, '<', 'input' or die $!; 
open my $output, '>', 'output' or die $!; 

binmode $input, ':utf8'; 

while (<$input>) { 
    s/£/&pound;/g; 
    print $output $_; 
} 
0
perl -i.bak -ne 's/£/&pound/g; print $_' file 
相关问题