在PERL中正确输出特殊字符(unicode)

时间:2014-10-10 12:42:17

标签: regex perl unicode character

我正在尝试获取目录中的所有文件名,并确定哪些名称包含特殊字符。我正在使用正则表达式

/[^-a-zA-Z0-9_.]/

示例文件(我使用触摸创建):

pdf-2014à014&7_06_64-Os_O&L,_Inc.pdf
pdf-20_06_04-O_OnLine,_Inc.pdf
pdf-20_0_0-Utà_d.wr.pdf
pdf-20_12_28-20.Mga_Grf.Fwd_Notice_KDJFI789&_JFK38.pdf
pdf-2_0_0-C_—_DUKE.pdf
pdf-2_1_3-f_s-M_F_D&A.pdf
pdf_-_2014à014&1007_0617_06264-O_O&L,_Inc.pdf

在匹配正则表达式中的模式名称之前,Perl可以输出正确的名称。是的perl能够以某种方式匹配特殊字符但输出字符时会发生变化。

* pdf-2_0_0-C_—_DUKE.pdf          >        pdf-2_0_0-C_???_DUKE.pdf

我可以尝试取消注释这一行

   #binmode(STDOUT, ":utf8");

再次运行命令脚本。当然可以?标记将被删除,但输出也不同。

* pdf-2_0_0-C_—_DUKE.pdf          >        pdf-2_0_0-C_â_DUKE.pdf

这是我的代码:

use strict;
use warnings;
use File::Find;
use Cwd;

#binmode(STDOUT, ":utf8");

my $starting_directory = cwd();

use Term::ANSIColor;

checkForSpecialChar(cwd());


sub checkForSpecialChar{
    my ($source_dir) = @_;

    chdir $source_dir or die qq(Cannot change into "$source_dir");

    find ( sub {
        return unless -f;   #We want files only
        print "\n";
        while(m/([^-a-zA-Z0-9_.])/g){ 
            chomp($_);
            print "DETECTED: |" . $_ . "|\n";
            print $`;
            print color 'bold red';
            print "$1";
            print color 'reset';
            print  $' . "\n";

        }

    }, ".");

    chdir("$starting_directory");

任何想法家伙?

更新: 嗯,你们是正确的看起来它不是正则表达式的问题。嗨AKHolland,我尝试将代码更改为与您的代码一样进行测试。但仍然产生相同的问题与hypen和一个小字母a-grave。 它给了我一个小字母而不是一个小字母 a`不使用binmode时(STDOUT,“:utf8”); aÌ使用binmode时(STDOUT,“:utf8”);

use strict;
use warnings;
use File::Find;
use Cwd;
use Encode;
binmode(STDOUT, ":utf8");

my $starting_directory = cwd();

use Term::ANSIColor;

checkForSpecialChar(cwd());


sub checkForSpecialChar{
   my ($source_dir) = @_;

   chdir $source_dir
       or die qq(Cannot change into "$source_dir");

   find ( sub {
      return unless -f;   #We want files only
     print $_ . "\n";
      $_ = Encode::decode_utf8($_);
      for(my $counter =0; $counter < length($_); $counter++) {
        print Encode::encode_utf8(substr($_,$counter,1)) .  "\n";
      } 

}, ".");

CHDIR( “$ starting_directory”); }

Output with

    binmode(STDOUT, ":utf8");

pdf-2_0_0-C_â_DUKE.pdf
p
d
f
-
2
_
0
_
0
-
C
_
â
_
D
U
K
E
.
p
d
f
pdf_-_2014aÌ014&1007_0617_06264-O_O&L,_Inc.pdf
p
d
f
_
-
_
2
0
1
4
a
Ì
0
1
4
&
1
0
0
7
_
0
6
1
7
_
0
6
2
6
4
-
O
_
O
&
L
,
_
I
n
c
.
p
d
f
OUTPUT without 
    binmode(STDOUT, ":utf8");

pdf-2_0_0-C_—_DUKE.pdf
p
d
f
-
2
_
0
_
0
-
C
_
—
_
D
U
K
E
.
p
d
f
pdf_-_2014à014&1007_0617_06264-O_O&L,_Inc.pdf
p
d
f
_
-
_
2
0
1
4
a
̀
0
1
4
&
1
0
0
7
_
0
6
1
7
_
0
6
2
6
4
-
O
_
O
&
L
,
_
I
n
c
.
p
d
f

2 个答案:

答案 0 :(得分:3)

您需要在途中对其进行解码并在出路时对其进行编码。像这样:

use Encode;
find ( sub {
    $_ = Encode::decode_utf8($_);
    while(m/([^-a-zA-Z0-9_.])/g){
        my $chr = Encode::encode_utf8($1);
        print "$chr\n"
    }
}, ".");

答案 1 :(得分:1)

中的字符pdf-2_0_0-C_—_DUKE.pdf在utf-8中使用3个字符编码:

char Unicode   UTF-8
—    U+2014    \xe2\x80\x94

所以,正如@AKHolland所说,你必须对它进行编码。

相关问题