如何在Perl中检测表情符号作为unicode?

Che*_*ikh 2 unicode perl emoji

我有一个文本文件,其中包含表情符号,☹️,,,,,,,的表情符号unicode caracter.

例如,代码\ N {1F60D}对应于我使用推荐,如https://perldoc.perl.org/perluniintro.html部分创建Unicode.我的程序必须检测它们并做一些治疗,但如果我使用

open(FIC1, ">$fic");

while (<FIC>) {
my $ligne=$_;

if( $ligne=~/\N{1F60D}/  )
{print "heart ";
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我这样做,它的工作

open(FIC1, ">$fic");

while (<FIC>) {
my $ligne=$_;

if( $ligne=~//  )
{print "Heart ";
    }
}
Run Code Online (Sandbox Code Playgroud)

第一个代码问候有什么问题

tin*_*ita 8

如果你看一下的perldoc perlre\N,你看,它的意思是"命名的Unicode字符或字符序列".

您可以使用此代替:

if ($ligne =~ m/\N{U+1F60D}/)
# or
if ($ligne =~ m/\x{1F60D}/)
Run Code Online (Sandbox Code Playgroud)

编辑:您在发布的链接中也有描述, https://perldoc.perl.org/perluniintro.html

编辑:您阅读的内容可能未被解码.你要:

use Encode;
...
my $ligne = decode_utf8 $_;
Run Code Online (Sandbox Code Playgroud)

或者直接以utf8模式打开文件:

open my $fh, "<:encoding(UTF-8)", $filename or die "Could not open $filename: $!";
while (my $ligne = <$fh>) {
    if ($ligne =~ m/\N{U+1F60D}/) { ... }
}
Run Code Online (Sandbox Code Playgroud)

你从未展示过如何打开所调用的文件句柄FIC,所以我认为它是utf8解码的.这是关于perl中unicode的另一个很好的教程:https://perlgeek.de/en/article/encodings-and-unicode

  • 我不仅添加了m.请仔细看看.我加了"U +" (2认同)

jm6*_*666 7

为了检测表情符号,我会在正则表达式中使用unicode属性,例如:

  • \p{Emoticons} 要么
  • \p{Block: Emoticons}

例如,只打印出表情符号

perl -CSDA -nlE 'say for( /(\p{Emoticons})/g )' <<< 'abc???'
Run Code Online (Sandbox Code Playgroud)

将打印




Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅perluniprops