本文介绍了当比较Perl中的字符串时,如何忽略重音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个测验应用程序,我匹配什么人类型与正确的答案。现在,我做的基本上是:
if($ input =〜/ $ answer / i){
打印you won;
}
很好,好像答案是鱼,用户可以键入一个鱼,并算作一个很好的答案。
我面临的问题是,好,我的用户,因为我是法国,我想能够接受,比方说,用户键入taton,答案是tâton。
所以,我可以做的是:
使用POSIX qw(locale_h);
使用locale;
setlocale(LC_TYPE,fr_FR.ISO8859-15);
setlocale(LC_COLLATE,fr_FR.ISO8859-15);
在我的检查程序中,执行:
$ input = lc($ input);
$ input =〜tr /àáâãååèèêêêìíîïñòóôõöùúûüÿÿ/ aaaaaaceeeeiiiinooooouuuuyy /;
和类似的答案。
我不喜欢它,因为我必须硬编码的东西,而我决定离开ISO-8859-15世界为UTF-8世界,我注定了。
所以,我正在寻找一种方法来比较字符串,将使tâtoneqtaton
,maçoneqmacon
或macon=〜/maçon/
div class =h2_lin>解决方案
尝试模块(或) )。
I have this quiz application where I match what people type with the right answer. For now, what I do is basically that :
if ($input =~ /$answer/i) {
print "you won";
}
It's nice, as if the answer is "fish" the user can type "a fish" and be counted a good answer.
The problem I'm facing is that, well, my users as I are french, and I'd like to be able to accept, say, a user typing "taton", and the answer being "tâton".
So, what I could do, is :
use POSIX qw(locale_h);
use locale;
setlocale(LC_TYPE, "fr_FR.ISO8859-15");
setlocale(LC_COLLATE, "fr_FR.ISO8859-15");
And in my check routine, do a :
$input = lc($input);
$input =~ tr/àáâãäåçèéêëìíîïñòóôõöùúûüýÿ/aaaaaaceeeeiiiinooooouuuuyy/;
and something likewise with the answer.
I don't like it, because I have to hard code things, and the day I decide I'm leaving the ISO-8859-15 world for the UTF-8 world, I'm doomed.
So, I'm looking for a way to compare strings, that will make "tâton" eq "taton"
, "maçon" eq "macon"
or "macon" =~ /maçon/
be true.
解决方案
Try the Text::Unaccent module from CPAN (or Text::Unaccent::PurePerl).
这篇关于当比较Perl中的字符串时,如何忽略重音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!