Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在perl中使用新行命令的编码问题_Regex_Perl_Encoding - Fatal编程技术网

Regex 在perl中使用新行命令的编码问题

Regex 在perl中使用新行命令的编码问题,regex,perl,encoding,Regex,Perl,Encoding,只要我只打印特殊字符,程序就可以运行。但我想把它们分开分类。使用新行命令,字符变为问号。有人能告诉我为什么以及如何解决这个问题吗 #!/usr/bin/perl while (<>) { while (/(.)/g) { if (ord($1) >= 128){ print "$1\n"; } } } #/usr/bin/perl 而(){ 而(/()/g){ 如果(作战需求文件($1)>=128){ 打印“$1\n”; }

只要我只打印特殊字符,程序就可以运行。但我想把它们分开分类。使用新行命令,字符变为问号。有人能告诉我为什么以及如何解决这个问题吗

#!/usr/bin/perl

while (<>) {
  while (/(.)/g) {
    if (ord($1) >= 128){
       print "$1\n";      
    }
  }
}
#/usr/bin/perl
而(){
而(/()/g){
如果(作战需求文件($1)>=128){
打印“$1\n”;
}
}
}

打开非ASCII文件时,应该告诉Perl文件的编码方式。同样,在打印这些字符时,您应该指定它们在输出时的编码方式

例如,要处理UTF-8编码字符,请在代码前添加以下内容:

use open IO => ':encoding(UTF-8)', ':std';

有关详细信息,请参阅。

如果您使用UNIX系统,则不清楚您的环境中有什么终端和语言环境设置

根据区域设置,并非所有符号都可以打印到控制台,您将看到。有些符号根本不打算打印(无法可视化的控制符号)

您有两个选择:

  • 调整区域设置以匹配使用的字符
  • 将输入和输出重新编码为区域设置支持的
此外,您的代码可能更容易阅读以下形式

use strict;
use warnings;
use feature 'say';

my $debug = 0;

while (<DATA>) {
    chomp;
    say     if $debug;
    map{ my $d = ord; print "[$d]" } split '';
    say ''  if $debug;;
}

__DATA__
use strict;
use warnings;
use feature 'say';

while (<>) {
    say;
    map{ my $d = ord; print "[$d]" if $d >= 128 } split '', $_;
}
使用严格;
使用警告;
使用特征“说”;
我的$debug=0;
而(){
咀嚼;
说如果$debug;
映射{my$d=ord;打印“[$d]”}拆分“”;
如果$debug;,则表示为“”;;
}
__资料__
严格使用;
使用警告;
使用特征“说”;
而(){
说
映射{my$d=ord;如果$d>=128,则打印“[$d]”拆分“”,$;
}

提示:
while(/()/g){if(ord($1)>=128){…}
可以写成
,而(/([^\x00-\x7F])/g){…}
是否有可能不匹配字符的概念(元字符),而应该匹配字形簇(
\X
)?
IO=>
是可选的