#!/usr/bin/env perl
use strict;
use warnings;
my $string = ";the;fox;jumped;over;the;dog;the;duck;and;the;frog;";
my $pattern = qr{;the
(?=;f
)};
my $count;
$count++ while $string =~ m{ (?&word){4} # Preceded by 4 words
(?:
(?= ( $pattern) ) (?&word) # Match Pattern at 5th word
|
(?&word) (*SKIP)(*FAIL) # Or consume 5th word and skip to next part of string.
)
(?(DEFINE)
(?<word> ;[^;]* )
)
}xg;
print "Number of Matches = $count\n";
IyEvdXNyL2Jpbi9lbnYgcGVybAp1c2Ugc3RyaWN0Owp1c2Ugd2FybmluZ3M7CgpteSAkc3RyaW5nICA9ICI7dGhlO2ZveDtqdW1wZWQ7b3Zlcjt0aGU7ZG9nO3RoZTtkdWNrO2FuZDt0aGU7ZnJvZzsiOwpteSAkcGF0dGVybiA9IHFyezt0aGUoPz07Zil9OwoKbXkgJGNvdW50OwoKJGNvdW50Kysgd2hpbGUgJHN0cmluZyA9fiBtewogICAgKD8md29yZCl7NH0gICAgICAgICAgICAgICAgICAgICAgICAgIyBQcmVjZWRlZCBieSA0IHdvcmRzCiAgICAoPzoKICAgICAgICAoPz0gKCAkcGF0dGVybikgKSAoPyZ3b3JkKSAgICAgICMgTWF0Y2ggUGF0dGVybiBhdCA1dGggd29yZAogICAgfAogICAgICAgICg/JndvcmQpICgqU0tJUCkoKkZBSUwpICAgICAgICAgIyBPciBjb25zdW1lIDV0aCB3b3JkIGFuZCBza2lwIHRvIG5leHQgcGFydCBvZiBzdHJpbmcuCiAgICApCiAgICAoPyhERUZJTkUpCiAgICAgICAgKD88d29yZD4gO1teO10qICApIAogICAgKQp9eGc7CgpwcmludCAiTnVtYmVyIG9mIE1hdGNoZXMgPSAkY291bnRcbiI7Cg==