我已经在这一段时间了,似乎无法解决这个问题。这就是我想要做的。给定三个单词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)。
答案 0 :(得分:2)
"\bwhat(\s*\b\w*\b\s*){0,5}the(\s*\b\w*\b\s*){0,5}hell"
适合我(在Ruby中)
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]
答案 1 :(得分: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
答案 2 :(得分:0)
在clojure中为我工作:
(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
根据Ben Hughes的模式。