RegEx - matching a set of words

I have been on this for a while and seem to be unable to handle this. Here is what I am trying to do. Given the three words word1, word2 and word3, I would like to build a regular expression that will match them in that order, but with a set of potential words between them (except for a new line).

For example, if I had the following:

word1 = what
word2 = the
word3 = hell

I would like to match the following lines with one match:

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

I thought I could do the following (let's say that between each word variable there were from 0 to 5 words):

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

Alas, no, this will not work. The important thing is that I have a way to specify the distance between words between words (where m is always <n).

+3
source share
3 answers
$ 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
+1
source

"\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]
+2

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

.

0
source

Source: https://habr.com/ru/post/1710727/


All Articles