post by yamamoto | 0 | 836 views

perl tips 文字列の英文を取り出す

#!/usr/bin/perl

use strict;

my $content = "阴阳学说 yin-yang theory 中国古代哲学理论范畴。阴阳的对立统一是天地万物运动变化的根本规律。";
print "$content\n";

my @latin = GetLatin($content);
if ($#latin>0){
print join("\/",@latin),"\n" ;

}else{
print "no words you want!\n";
}

sub GetLatin {
my $string =shift;
$string.=" ";

my @alph_list = ('A' .. 'Z', 'a' .. 'z','\'','.','(',')','-' );
my @string = ();
my @word = ();
foreach my $character (split //, $string) {
if ( grep {$_ eq $character} @alph_list ){
push(@string, $character);
} else {
if ($#string>0){
push(@word, join(/ /,@string));
@string = ();
}

}
}
return @word;
}

実行結果
阴阳学说 yin-yang theory 中国古代哲学理论范畴。阴阳的对立统一是天地万物运动变化的根本规律。
yin-yang/theory

Comments are closed.