2017-04-17 15 views
1

我有一個文本文件和一個包含單詞列表的數組。我需要找到一種方式,在這種情況下,我可以過濾掉出現超過1個句子的句子。我只是無法制定如何編寫代碼。這裏有一個例子:Perl - 捕獲一個數組中出現多個元素的句子

輸入:

my @strings = (
    "i'm going to find the occurrence of two words if possible", 
    "i'm going to find the occurrence of two words if possible", 
    "to find a solution to this problem", 
    "i will try my best for a way to this problem" 
); 

my @words = ("find", "two", "way"); 

輸出:

i'm going to find the occurrence of two words if possible 
i'm going to find the occurrence of two words if possible 

而且我也知道這是一個簡單的問題,但我的心似乎已經打了路障。

+0

它應該與「find find」匹配嗎? – ikegami

+0

理想情況下,它應該匹配'find'和'two',但'find'和'find'也是合理的。 – ary

+0

這是一個是或否的問題... – ikegami

回答

1

如果你想使用關鍵字的兩個或多個實例的字符串:

my @keywords = ("find", "two", "way"); 
my %keywords = map { $_ => 1 } @keywords; 

for my $string (@strings) { 
    my @words = $string =~ /\w+/g; 
    my $count = grep { $keywords{$_} } @words; # Count words that are keywords. 
    if ($count >= 2) { 
     ... 
    } 
} 

短路交替(即好極長的字符串):

my @keywords = ("find", "two", "way"); 
my %keywords = map { $_ => 1 } @keywords; 

for my $string (@strings) { 
    my $count = 0; 
    while ($string =~ /\w+/g) { 
     if ($keywords{$_} && ++$count == 2) { 
     ... 
     last; 
     } 
    } 
} 

如果你想具有兩個或多個關鍵字實例的字符串:

my @keywords = ("find", "two", "way"); 

for my $string (@strings) { 
    my @words = $string =~ /\w+/g; 
    my %seen; ++$seen{$_} for @words; 
    my $count = grep { $seen{$_} } @keywords; # Count keywords that were seen. 
    if ($count >= 2) { 
     ... 
    } 
} 

候補:

my @keywords = ("find", "two", "way"); 

for my $string (@strings) { 
    my @words = $string =~ /\w+/g; 
    my %seen = map { $_ => -1 } @keywords; 
    my $count = grep { ++$seen{$_} == 0 } @words; 
    if ($count >= 2) { 
     ... 
    } 
} 

的短路交替(即適用於極長的琴絃):

my @keywords = ("find", "two", "way"); 

for my $string (@strings) { 
    my $count = 0; 
    my %seen = map { $_ => -1 } @keywords; 
    while ($string =~ /\w+/g) { 
     if (++$seen{$_} == 0 && ++$count == 2) { 
     ... 
     last; 
     } 
    } 
} 
+0

好吧還有一個查詢可以使用標籤\ Q和\ E,因爲在我的數組中可能有需要匹配的短語。 – ary

+0

呵呵?你會在哪裏放置'\ Q'和'\ E'? – ikegami

+0

我問,因爲我將不得不逃脫特殊字符因此,查詢。 – ary

相關問題