有没有更好的方法来匹配除这种方法以外的单词,即时通讯试图找到任何句子中出现的数组中的单词。如何用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