0

「语言中心」などの中国語の単語と一連の Web ファイル (php、html、htm など) を照合しています。

ただし、どういうわけか次のエラーが発生します。

Malformed UTF-8 character (1 byte, need 2, after start byte 0xdf) in regexp compilation at ../Final_FindOnlyNoReplace_CLE_Chinese.pl line 89, <INFILE> line 12.

誰でも助けることができますか?

これが私のコードです。

#!/usr/bin/env perl
use Encode qw/encode decode/;

use utf8;
use strict;
use Cwd;
use LWP::UserAgent;

my($path) = @_;

## append a trailing / if it's not there
$path .= '/' if($path !~ /\/$/);

use File::Glob ':glob';

my @all_files = bsd_glob($path."*");

for my $eachFile (@all_files) {
    open(INFILE, "<$eachFile") || die ("Could not open '$eachFile'\n");

    my(@inlines) = <INFILE>;
    my($line, $find);
    my $outkey = 1;

    foreach $line (@inlines) {
        $find = &find($line);
        if ($find ne 'false') {
            chomp($line);
            print "\tline$outkey : $line\n"; 
        }
        $outkey ++;
    }
}

#subroutine
sub find {
    my $m = encode("utf8", decode("big5", @_));

    my $html = LWP::UserAgent->new
        ->get($m)
        ->decoded_content;
    my $str_chinese = '語言中心';

    if ($m =~ /$str_chinese/) {  
        $m; ##if match, return the whole line.
    }
}   
4

2 に答える 2

0
#!/usr/bin/env perl
use utf8;
use strictures;
use LWP::UserAgent qw();
use Path::Class::Rule qw();
use URI::file qw();

my $start_directory = q(.);
my $search_text = qr'語言中心';

my $next = Path::Class::Rule->new->name(qw(*.php *.htm*))->iter($start_directory);

my @matching_lines;
while (my $file = $next->()) {
    for my $line (split /\R/, LWP::UserAgent
        ->new
        ->get(URI::file->new_abs($file))
        ->decoded_content
    ) {
        push @matching_lines, $line if $line =~ $search_text;
    }
}
# @matching_lines is (
#     '<title>Untitled 語言中心 Document</title>',
#     'abc 語言中心 cde',
#     '天天向上語言中心他'
# )
于 2012-06-22T10:51:12.853 に答える
0

$html取得してデコードしたものではなく、代わりに URL: で検索していますが$m =~ /$str_chinese/、これは意図したものではないと思います。

findまた、関数の結果を正確な文字列「false」と比較していますが、これは決して機能しません。わかりやすくするために、成功と失敗の明示的な戻り値を に変更if ($find ne 'false')して追加します。if (defined($find))find

最後に、他のファイルの中に他の Perl スクリプトがあるディレクトリを指しているため、スクリプトが失敗したようです。それらは UTF-8 である可能性が最も高いため、スクリプトがそれらを big5 データとして読み取ろうとすると、デコードに失敗します。データファイルのみをカバーするようにグロブを変更するだけです。

于 2012-06-22T10:09:52.973 に答える