【问题标题】:charnames, substitutions and Umlaut "Ü": Malformed UTF-8 character字符名、替换和元音变音“Ü”:格式错误的 UTF-8 字符
【发布时间】:2011-10-04 08:06:13
【问题描述】:

我正在处理使用控制字符进行文本分隔的书目记录。当我在替代操作中使用 Unicode 字符名称“STRING TERMINATOR”时,如果文本包含“Ü”,则会收到“格式错误的 UTF-8 字符”警告。替换适用于其他 Unicode 字符名称和包含“ü”、“ä”、“é”、...的文本。

#!/usr/bin/perl

use v5.12;

use utf8;
use strict;
use autodie;
use warnings;
# use warnings    qw< FATAL  utf8     >;
use charnames   qw< :full >;
use feature     qw< unicode_strings >;

binmode STDOUT, ':utf8';

my @records = (qq[\N{U+0098}L'\N{U+009c} Année du Figaro], qq[\N{U+0098}The\N{U+009c} famous ümläut], qq[\N{U+0098}The\N{U+009c} famous Ümlaut], qq[\N{U+0098}\N{U+00DC}\N{U+009c}]);

my %replace = (
    "\N{START OF STRING}"     => "<ns>",
    "\N{STRING TERMINATOR}"   => "</ns>",
);
my $regex = join "|", keys %replace;
$regex = qr/$regex/;

foreach my $record (@records){
    $record =~ s/($regex)/$replace{$1}/g;
    say $record;
};

我使用 Strawberry Perl v5.12.3.0。

如何避免这些警告?

谢谢,乔罗

【问题讨论】:

    标签: regex perl unicode utf-8 diacritics


    【解决方案1】:

    升级到 5.14,it won't throw warnings there

    【讨论】:

      【解决方案2】:

      如果可能,您可以在哈希中列出所有 Unicode 字符并在处理之前将其替换,以避免您拥有的 Perl 版本出现警告

      类似的,

      my $Description = "famous Ümlaut";
      my %UMLAUTE = ( 
          'Ä' => 'Ae',
          'Ö' => 'Oe', 
          'Ü' => 'Ue',
          'ä' => 'ae',
          'ö' => 'oe', 
          'ü' => 'ue', 
          'ß' => 'ss',
          'é' => 'e' 
      );
      
      my @UMLKEYS = join("|", keys(%UMLAUTE));
      $Description =~ s/(@UMLKEYS)/$UMLAUTE{$1}/g; 
      

      这将导致“著名的 Uemlaut”

      【讨论】:

        猜你喜欢
        • 2013-12-28
        • 2016-10-14
        • 1970-01-01
        • 2016-11-13
        • 2014-09-22
        • 2019-09-27
        • 2015-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多