使用perl将Fraktur符号转换为实体转换

时间:2016-11-21 09:39:29

标签: perl entity

我需要将Fraktur符号转换为Named实体:

my $input = ", ,  is a symbols of fraktur";

预期产出:

$output = "𝔄, 𝔅, 𝔇 is a symbols of fraktur";

代码:

use Win32;
use open ':encoding(utf8)';
binmode STDOUT, ':utf8';

my %fraktursChr = ("𝔄" => "", "𝔅" => "", "𝔇" => "");

foreach my $key (keys %fraktursChr)
{
    print "Key: $key-->$fraktursChr{$key}\n ";
    $input=~s/\Q$fraktursChr{$key}\E/$key/g;
}

但是我无法使用utf8替换符号:如果有人可以帮助我,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

需要添加

use utf8;来告诉Perl源代码使用UTF-8进行编码。

use utf8;                              # Source code is encoded using UTF-8.
use open ':std', ':encoding(UTF-8)';   # Terminal expects/provides UTF-8.

my $input = ",  , is a symbols of fraktur";
my %fraktursChr = ("𝔄" => "", "𝔅" => "", "𝔇" => "",);
foreach my $key (keys %fraktursChr)
{
    $input=~s/\Q$fraktursChr{$key}\E/$key/g;
}

print $input;

感谢PerlDuckDave Cross

答案 1 :(得分:1)

实际上不是答案,而是提高绩效的建议:

my $fraktursChr_pat = join '|', map quotemeta, keys %fraktursChr;
my $fraktursChr_re = qr/($fraktursChr_pat)/;

$input =~ s/$fraktursChr_re/$fraktursChr{$1}/g;