【发布时间】:2010-10-23 00:53:03
【问题描述】:
我有一个我想在其上进行正则表达式的网站,比如 http://www.ru.wikipedia.org/wiki/perl 。该网站是俄语的,我想提取所有俄语单词。与\w+ 匹配不起作用,与\p{L}+ 匹配会检索所有内容。
我该怎么做?
【问题讨论】:
-
这正是 Unicode 属性的用途。使用 \p{西里尔}。
我有一个我想在其上进行正则表达式的网站,比如 http://www.ru.wikipedia.org/wiki/perl 。该网站是俄语的,我想提取所有俄语单词。与\w+ 匹配不起作用,与\p{L}+ 匹配会检索所有内容。
我该怎么做?
【问题讨论】:
所有这些答案都过于复杂。使用这个
$text =~/\p{cyrillic}/
砰。
【讨论】:
$text =~ /\p{Cyrillic}/
perl -MLWP::Simple -e 'getprint "http://ru.wikipedia.org/wiki/Perl"'
403 Forbidden <URL:http://ru.wikipedia.org/wiki/Perl>
好吧,那没用!
先下载一份,好像可以了:
use Encode;
local $/ = undef;
my $text = decode_utf8(<>);
my @words = ($text =~ /([\x{0400}-\x{04ff}]+)/gs);
foreach my $word (@words) {
print encode_utf8($word) . "\n";
}
【讨论】:
\x{0401}-\x{042f} 用于俄语单词。无需过度匹配字符。检查unicode表here
好的,那就试试这个:
#!/usr/bin/perl
use strict;
use warnings;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $response = $ua->get("http://ru.wikipedia.org/wiki/Perl");
die $response->status_line unless $response->is_success;
my $content = $response->decoded_content;
my @russian = $content =~ /\s([\x{0400}-\x{052F}]+)\s/g;
print map { "$_\n" } @russian;
我相信西里尔文字符集以0x0400 开头,而西里尔文补充字符集以0x052F 结尾,所以这应该得到很多单词。
【讨论】:
把这个留在这里。 匹配特定的俄语单词
use utf8;
...
utf8::decode($text);
$text =~ /привет/;
【讨论】: