2009-06-18 202 views
3

我一直在這一段時間,似乎無法解決它。這是我想要做的。給定三個單詞word1,word2和word3,我想構建一個正則表達式,它將按順序匹配它們,但它們之間有一組潛在的單詞(除了新行)。正則表達式 - 匹配一組詞

舉例來說,如果我有以下幾點:

word1 = what 
word2 = the 
word3 = hell 

我想匹配以下字符串,用一根火柴:

"what the hell" 
"what in the hell" 
"what the effing hell" 
"what in the 9 doors of hell" 

我想我可以做到以下幾點(允許每個單詞變量之間存在0到5個單詞):

regex = "\bword1(\b\w+\b){0,5}word2(\b\w+\b){0,5}word3\b" 

唉,不,它不起作用。重要的是我可以指定單詞之間的m到n個單詞的距離(其中m總是< n)。

回答

1
$ cat try 
#! /usr/bin/perl 

use warnings; 
use strict; 

my @strings = (
    "what the hell", 
    "what in the hell", 
    "what the effing hell", 
    "what in the 9 doors of hell", 
    "hello", 
    "what the", 
    " what the hell", 
    "what the hell ", 
); 

for (@strings) { 
    print "$_: ", /^what(\s+\w+){0,5}\s+the(\s+\w+){0,5}\s+hell$/ 
        ? "match\n" 
        : "no match\n"; 
} 

$ ./try 
what the hell: match 
what in the hell: match 
what the effing hell: match 
what in the 9 doors of hell: match 
hello: no match 
what the: no match 
what the hell: no match 
what the hell : no match 
+0

這是迄今爲止最優雅的作品,它的功能與廣告中的一樣,但有次要的匹配。你告訴我,我關心那件事嗎?我最關心的是整個字符串與前面的word1,中間的word2以及末尾的word3(「中間的某個地方」是單詞距離問題)匹配。 – 2009-06-18 01:45:38

2

"\bwhat(\s*\b\w*\b\s*){0,5}the(\s*\b\w*\b\s*){0,5}hell"作品我(紅寶石)Clojure中

list = ["what the hell", "what in the hell", "what the effing hell", 
    "what in the 9 doors of hell", "no match here hell", "what match here hell"] 

list.map{|i| /\bwhat(\s*\b\w*\b\s*){0,5}the(\s*\b\w*\b\s*){0,5}hell/.match(i) } 
=> [#<MatchData:0x12c4d1c>, #<MatchData:0x12c4d08>, #<MatchData:0x12c4cf4>, 
    #<MatchData:0x12c4ce0>, nil, nil] 
+0

這是匹配整個短語並返回組的結果(1)。 我也試過(\ s * \ w * \ s *){0,5},結果相同。這比我自己做得更多!有什麼建議麼?我在Python中這樣做,以防萬一。 – 2009-06-18 01:31:00

0

工作對我來說:

(def phrases ["what the hell" "what in the hell" "what the effing hell" 
       "what in the 9 doors of hell"]) 

(def regexp #"\bwhat(\s*\b\w*\b\s*){0,5}the(\s*\b\w*\b\s*){0,5}hell") 

(defn valid? [] 
    (every? identity (map #(re-matches regexp %) phrases))) 

(valid?) ; <-- true 

按照本休斯的格局。