作者:墨镜DHED_304 | 来源:互联网 | 2023-01-04 10:41
我有一个文本文件,其中包含表情符号,☹️,,,,,,,的表情符号unicode caracter.
例如,代码\ N {1F60D}对应于我使用推荐,如https://perldoc.perl.org/perluniintro.html部分创建Unicode.我的程序必须检测它们并做一些治疗,但如果我使用
open(FIC1, ">$fic");
while () {
my $ligne=$_;
if( $ligne=~/\N{1F60D}/ )
{print "heart ";
}
}
现在我这样做,它的工作
open(FIC1, ">$fic");
while () {
my $ligne=$_;
if( $ligne=~// )
{print "Heart ";
}
}
第一个代码问候有什么问题
1> tinita..:
如果你看一下的perldoc perlre的\N
,你看,它的意思是"命名的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
我不仅添加了m.请仔细看看.我加了"U +"
2> jm666..:
为了检测表情符号,我会在正则表达式中使用unicode属性,例如:
\p{Emoticons}
要么
\p{Block: Emoticons}
例如,只打印出表情符号
perl -CSDA -nlE 'say for( /(\p{Emoticons})/g )' <<<'abc???'
将打印
有关更多信息,请参阅perluniprops