Question

I'm looking to create a Regex which meets the following requirements:

1) Must act as an "AND" statement

2) Both words should be in a range of eachother

3) It does not count two of the same word.

So far, I have this working REGEX, which satisfies 1 and 2.

/(word1|word2)(?:\W+\w+){0,3}?\W+(word1|word2)/i

Example Regex:
/(cat|dog)(?:\W+\w+){0,3}?\W+(cat|dog)/i

Strings That Work Now

  • The cat scared the other cat.

  • The cat likes the dog.

  • The dog likes the cat.

  • The dog hates the dog.

Strings That I Don't Want

  • The cat scared the other cat.

  • The dog hates the dog.

A phrase such as "The cat scared the other cat." would match this REGEX because it is searching for any word in the second grouping, which includes cat. I do not want it to search for itself, however. I want it only to search for dog.

Was it helpful?

Solution

How about:

/(cat|dog)(?:\W+\w+){0,3}?\W+(?!\1)(cat|dog)/

Explanation:

The regular expression:

(?-imsx:(cat|dog)(?:\W+\w+){0,3}?\W+(?!\1)(cat|dog))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    cat                      'cat'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    dog                      'dog'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?:                      group, but do not capture (between 0 and 3
                           times (matching the least amount
                           possible)):
----------------------------------------------------------------------
    \W+                      non-word characters (all but a-z, A-Z,
                             0-9, _) (1 or more times (matching the
                             most amount possible))
----------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  ){0,3}?                  end of grouping
----------------------------------------------------------------------
  \W+                      non-word characters (all but a-z, A-Z, 0-
                           9, _) (1 or more times (matching the most
                           amount possible))
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    \1                       what was matched by capture \1
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    cat                      'cat'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    dog                      'dog'
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top