如何将带有重音符号,变音符号等的字母转换为Perl中的ASCII对应字母?

时间:2012-06-15 21:08:19

标签: perl ascii diacritics data-conversion

我正在编写一个在Perl中处理文档的程序,很多文档都有ä, ö, ü, é, etc(大写和小写)等字符。我想用ASCII对应a, o, u, e, etc替换它们。我怎么能在Perl中做到这一点?

我想到的一个解决方案是使用键是变音符号和重音字符的哈希值,并且值是ASCII对应物,但是这需要我有一个所有变音符号和重音符号的列表,我不知道如果我建立了一个清单,我肯定会想念很多,因为我不熟悉所有可能有变音符号,重音符号和其他变音符号的字符。

4 个答案:

答案 0 :(得分:24)

像往常一样,如果你想到的问题肯定不是你的问题,那么CPAN已经有了一个解决方案。 )在这种情况下,它被称为Text::Unidecode

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

答案 1 :(得分:3)

Text::Unidecode

请参阅许多免责声明,但如果您只使用带有变音符号的拉丁文本,则可能正是您所需要的。

答案 2 :(得分:1)

use s/// (=Search&Replace) instead of m// (=Match)

e.g. $name =~ s/\x00c0/A/g;

答案 3 :(得分:-1)

我做了这个子程序,我通过它提供每个单词。这可能很慢。

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