如何在Perl中将emoji检测为Unicode? [英] How to detect emoji as unicode in Perl?
本文介绍了如何在Perl中将emoji检测为Unicode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本文件,其中包含例如😤,☹️,😔,😅,😃,😉,😜,😍的表情符号UNICODE字符。
例如,代码N{1F60D}对应于😍 我使用https://perldoc.perl.org/perluniintro.html创建Unicode一节中的推荐。 我的程序必须检测到它们并进行一些处理,但如果我使用open(FIC1, ">$fic");
while (<FIC>) {
my $ligne=$_;
if( $ligne=~/N{1F60D}/ )
{print "heart ";
}
}
现在我这样做了,它起作用了
open(FIC1, ">$fic");
while (<FIC>) {
my $ligne=$_;
if( $ligne=~/😍/ )
{print "Heart ";
}
}
第一个代码有什么问题 问候
推荐答案
如果您查看N
的perldoc perlre,您会发现它的意思是"命名的unicode字符或字符序列"。
您可以改用:
if ($ligne =~ m/N{U+1F60D}/)
# or
if ($ligne =~ m/x{1F60D}/)
编辑:在您发布的链接中也有描述, https://perldoc.perl.org/perluniintro.html
编辑: 您阅读的内容可能没有被解码。您需要:
use Encode;
...
my $ligne = decode_utf8 $_;
或直接在UTF8模式下打开文件:
open my $fh, "<:encoding(UTF-8)", $filename or die "Could not open $filename: $!";
while (my $ligne = <$fh>) {
if ($ligne =~ m/N{U+1F60D}/) { ... }
}
您从未展示过如何打开名为FIC
的文件句柄,所以我假设它是UTF8解码的。
下面是关于Perl中的Unicode的另一个很好的教程:https://perlgeek.de/en/article/encodings-and-unicode
这篇关于如何在Perl中将emoji检测为Unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文