如何转换在Perl中的口音,变音符号等,以它们的ASCII同行的信吗?如何转换在Perl中的口音,变

2019-06-01 03:40发布

我正在写一个程序有字符,如与在Perl文件和大量文件的工作ä, ö, ü, é, etc (包括大小写)。 我想用ASCII同行来替换它们a, o, u, e, etc 。 我会怎么做在Perl?

一个我认为的解决方案就是与键在变音符号和重音字符和值是ASCII对应一个哈希,但这需要我有所有的变音符号和重音字符,这是我没有一个列表,如果我建了一个名单,我肯定会错过很多因为我不熟悉所有可能有变音,重音和其他变音符号的可能的字符。

Answer 1:

像往常一样,如果你认为有问题的这肯定不是你的唯一,还有已经在CPAN的解决方案。 )在这种情况下,它被称为文本:: Unidecode

use warnings;
use strict;
use utf8;
use Text::Unidecode;
print unidecode('ä, ö, ü, é'); # will print 'a, o, u, e'


Answer 2:

Text::Unidecode

看到很多免责条款,但它可能是您需要的,如果你只是有变音符号拉丁文字。



Answer 3:

使用s/// (=搜索和替换)而不是m// (=匹配)

例如$name =~ s/\x00c0/A/g;



Answer 4:

我这样做的子程序,我通过它互相喂字。 这可能是缓慢的。

sub store_utf82_encoding{
##see file UTF8vowels.txt
#converts  UTF8 Euro vowels to nearest English equivant  

  my $name=$_[0];
  $name =~m/\x00c0/A/g; #Agrav
  $name =~m/\x00c1/A/g; # Aacute
  $name =~m/\x00c2/A/g; # Acap
  $name =~m/\x00c3/A/g; # Atilde
  $name =~m/\x00c4/A/g; # Auml
  $name =~m/\x00c5/A/g; # Aring
  $name =~m/\x00c6/AE/g; # AE
  $name =~m/\x00c7/Ch/g; # Ccedilla
  $name =~m/\x00c8/E/g; #Egrav
  $name =~m/\x00c9/E/g; # Eacute
  $name =~m/\x00ca/E/g; # Ecap
  $name =~m/\x00cb/E/g; # Euml
  $name =~m/\x00cc/I/g; # Igrav
  $name =~m/\x00cd/I/g; # Iacut
  $name =~m/\x00ce/I/g; # Icap
  $name =~m/\x00cf/I/g; # Iuml
  $name =~m/\x00d0/Th/g; #CapEth
  $name =~m/\x00d1/NY/g; # Ntild
  $name =~m/\x00d2/O/g; # Ograv
  $name =~m/\x00d3/O/g; # Oacute
  $name =~m/\x00d4/O/g; # Ocap
  $name =~m/\x00d5/Th/g; # Otilde
  $name =~m/\x00d6/O/g; # Ouml
  $name =~m/\x00d8/O/g; # Ostroke 
  $name =~m/\x00d9/U/g; # Ugrav
  $name =~m/\x00da/U/g; # Uacute
  $name =~m/\x00db/U/g; # Ucap
  $name =~m/\x00dc/U/g; # Uuml
  $name =~m/\x00dd/Y/g; # Yacute
  $name =~m/\x00de/Th/g; # CapThorn
  $name =~m/\x00df/SS/g; # GermanUCss Ezette
  $name =~m/\x00e0/a/g; # agrav
  $name =~m/\x00e1/a/g; # aacute 
  $name =~m/\x00e2/a/g; # acap
  $name =~m/\x00e3/a/g; # atilde
  $name =~m/\x00e4/a/g; # auml
  $name =~m/\x00e5/a/g; # aring
  $name =~m/\x00e6/ae/g; # ae
  $name =~m/\x00e7/ch/g; # ccedilla 
  $name =~m/\x00e8/e/g; # egrav
  $name =~m/\x00e9/e/g; # eacute
  $name =~m/\x00ea/e/g; # ecap
  $name =~m/\x00eb/e/g; # euml
  $name =~m/\x00ec/i/g; # igrav
  $name =~m/\x00ed/i/g; # iacute
  $name =~m/\x00ee/i/g; # icap
  $name =~m/\x00ef/i/g; # iuml
  $name =~m/\x00f0/th/g; # lowercase eth
  $name =~m/\x00f1/ny/g; # ntilde
  $name =~m/\x00f2/o/g; # ograv
  $name =~m/\x00f3/o/g; # oacute 
  $name =~m/\x00f4/o/g; # ocap
  $name =~m/\x00f5/th/g; # otilde
  $name =~m/\x00f6/o/g; # ouml
  $name =~m/\x00f8/o/g; # ostroke
  $name =~m/\x00f9/u/g; # ugrav
  $name =~m/\x00fa/u/g; # uacute
  $name =~m/\x00fb/u/g; # ucap
  $name =~m/\x00fc/u/g; # uuml
  $name =~m/\x00fe/th/g; # lowercase thorn
  $name =~m/\x00fd/y/g; # yacute
  $name =~m/\x00ff/y/g; # yuml

return $name;

} #endsub store_utf82_encoding


文章来源: How to convert letters with accents, umlauts, etc to their ASCII counterparts in Perl?