问题
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.
回答1:
Try the Text::Unaccent module from CPAN (or Text::Unaccent::PurePerl).
回答2:
This does not seem like a proper occasion for invoking regular expressions--you should simply have a list of acceptable answers, plus some filtering to remove nonessential words like "a", "the", and their language-specific equivalents.
Whatever you do, it seems obvious to me that it must be character-encoding-aware and language-aware. Regular expressions are typically neither.
来源:https://stackoverflow.com/questions/386459/how-can-i-ignore-accents-when-comparing-strings-in-perl