2009-07-06 68 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 
相關問題