有沒有更好的方法來匹配除這種方法以外的單詞,即時通訊試圖找到任何句子中出現的數組中的單詞。如何用Perl匹配一個句子中的順序詞?
my $count = 0;
my @strings = (
"i'm going to find the occurrence of two words going if possible",
"i'm going to find the occurrence of two words if impossible",
"to find a solution to this problem",
"i will try my best for a way to match this problem"
);
@neurot = qw(going match possible);
my $com_neu = '\b'.join('\b|\b', @neurot).'\b';
foreach my $sentence (@string){
@l = $sentence =~ /($com_neu)/gi;
foreach my $list (@l){
if($list =~ m/\w['\w-]*/){
print $list;
$count++;
}
}
print $count;
}
輸出:
String 1: going going possible
String 2: going
String 3:
String 4: match
,請幫助我更快的方法。
謝謝。
對於初學者來說,你不需要'\ B'周圍的每一個字,就在括號:'\ B($ com_neu)\ B'。 – TLP
你應該提供一些關於你的數據和句子的更多信息('@ neurot'中有多少單詞,句子多長......)。 – bvr
m/\ w /將匹配所有與m/\ w ['\ w - ] */will相同的字符串。那麼['\ w - ] *部分的重點是什麼? – tadmc