14

我正在编写一个使用 Perl 处理文档的程序,并且很多文档都有诸如ä, ö, ü, é, etc(大写和小写)之类的字符。我想用 ASCII 对应物替换它们a, o, u, e, etc。我将如何在 Perl 中做到这一点?

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

4

4 回答 4

27

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

use warnings;
use strict;
use utf8;
use Text::Unidecode;
print unidecode('ä, ö, ü, é'); # will print 'a, o, u, e'
于 2012-06-15T21:19:15.620 回答
3

Text::Unidecode

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

于 2012-06-15T21:18:44.440 回答
1

使用s/// (=Search&Replace)而不是m// (=Match)

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

于 2018-04-18T18:19:30.180 回答
0

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

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
于 2017-01-22T18:03:48.217 回答