in reply to Regex Problem

That's a non-trivial problem. And the non-triviality in it lies in the definition of "word". What is a word to you? Is "can't" a word? Is it two words? What about "Smith-Jones"? One word? Or two?

Now, suppose you have regexes for words, and strings of non-word characters, it becomes easy:

my $word_pat = qr /.../; my $non_word_pat = qr /.../; if (/word1${non_word_pat}(?:${word_pat}${non_word_pat}){0,4}word2/) { ... no more than 4 words between word1 and word2 ... }
But you have to create the $word_pat and $non_word_pat patterns yourself, as I do not know what you consider a word, and what not.

Replies are listed 'Best First'.
Re^2: Regex Problem
by remluvr (Sexton) on Oct 24, 2008 at 08:46 UTC
    We decided to take words made up of just one actual word. So can't is not a word and neither is Smith-Jones.
    As for what I've tried is not much, really:
    \s?word1 [\s \w{1,20}]{0,4} word2
    but I guess I messed up pretty badly..
      So, if "can't" is not a word, then you want a match on:
      word1 one two three can't can't can't can't can't can't word2
      because there are no more than 3 words between word1 and word2?