[PerlChina] 如何用正则确定变量的内容是utf8还是gb2312的?

silent silent2600 at gmail.com
Wed Nov 26 22:23:47 PST 2008


看了cu上的帖子:
http://bbs.chinaunix.net/viewthread.php?tid=907172

于是:

sub is_utf8 {
my $r = shift;
return 1 if ($$r
=~/[\x01-\x7f]|[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/);
return 0;
}

sub is_gbk {
my $r = shift;

return 1 if ($$r=~/[\xb0-\xf7][\xa0-\xfe]/);
return 0;
}

可是不准确, 一段文字是utf8或是gb2312的都同时可以被成功匹配

为何?


More information about the China-pm mailing list