fork download
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $string = ";the;fox;jumped;over;the;dog;the;duck;and;the;frog;";
  6. my $pattern = qr{;the(?=;f)};
  7.  
  8. my $count;
  9.  
  10. $count++ while $string =~ m{
  11. (?&word){4} # Preceded by 4 words
  12. (?:
  13. (?= ( $pattern) ) (?&word) # Match Pattern at 5th word
  14. |
  15. (?&word) (*SKIP)(*FAIL) # Or consume 5th word and skip to next part of string.
  16. )
  17. (?(DEFINE)
  18. (?<word> ;[^;]* )
  19. )
  20. }xg;
  21.  
  22. print "Number of Matches = $count\n";
  23.  
Success #stdin #stdout 0s 3740KB
stdin
Standard input is empty
stdout
Number of Matches = 1