如何用Perl替换文件中的所有£?

时间:2009-07-06 08:21:43

标签: regex perl

我正在尝试用£替换HTML文件中的所有£符号。我的正则表达似乎不起作用。

你能帮忙吗?

3 个答案:

答案 0 :(得分: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 $_;
}

答案 1 :(得分: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

答案 2 :(得分:0)

perl -i.bak -ne 's/£/&pound/g; print $_' file