如何在 Perl 中将表情符号检测为 unicode?
How to detect emoji as unicode in Perl?
我有包含 emoji 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 ";
}
}
第一个代码有什么问题
此致
如果您在 perldoc perlre 中查看 \N
,您会发现它表示 "named Unicode character or character sequence"。
您可以改用这个:
if ($ligne =~ m/\N{U+1F60D}/)
# or
if ($ligne =~ m/\x{1F60D}/)
编辑:在您发布的 link 中也有描述,
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 -C
可用于启用 unicode 功能
perl -C -E 'say "\N{U+263a}"'|perl -C -ne 'print if /\N{U+263a}/'
-C [number/list]
The -C flag controls some of the Perl Unicode features.
...
第二个代码起作用的原因是 perl 匹配 UTF-8 二进制序列:如 perl -ne 'print if /\xf0\x9f\x98\x8d/'
.
以下应该有效
#!/usr/bin/perl -C
open(FIC1, ">$fic");
while (<FIC>) {
my $ligne=$_;
if( $ligne=~/\N{U+1F60D}/ ) {
print "heart ";
}
}
为了检测表情符号,我会在正则表达式中使用 unicode 属性,例如:
\p{Emoticons}
或
\p{Block: Emoticons}
例如,只打印表情符号
perl -CSDA -nlE 'say for( /(\p{Emoticons})/g )' <<< 'abcαβγ'
将打印
有关详细信息,请参阅 perluniprops
我有包含 emoji 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 ";
}
}
第一个代码有什么问题 此致
如果您在 perldoc perlre 中查看 \N
,您会发现它表示 "named Unicode character or character sequence"。
您可以改用这个:
if ($ligne =~ m/\N{U+1F60D}/)
# or
if ($ligne =~ m/\x{1F60D}/)
编辑:在您发布的 link 中也有描述, 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 -C
可用于启用 unicode 功能
perl -C -E 'say "\N{U+263a}"'|perl -C -ne 'print if /\N{U+263a}/'
-C [number/list]
The -C flag controls some of the Perl Unicode features. ...
第二个代码起作用的原因是 perl 匹配 UTF-8 二进制序列:如 perl -ne 'print if /\xf0\x9f\x98\x8d/'
.
以下应该有效
#!/usr/bin/perl -C
open(FIC1, ">$fic");
while (<FIC>) {
my $ligne=$_;
if( $ligne=~/\N{U+1F60D}/ ) {
print "heart ";
}
}
为了检测表情符号,我会在正则表达式中使用 unicode 属性,例如:
\p{Emoticons}
或\p{Block: Emoticons}
例如,只打印表情符号
perl -CSDA -nlE 'say for( /(\p{Emoticons})/g )' <<< 'abcαβγ'
将打印
有关详细信息,请参阅 perluniprops