#!/usr/bin/perl use strict; use warnings; while () { my $line = $_; my $not = ''; if ($line !~ /word1(([,\s]+\w+)?){1,4}\s+word2/) { $not = 'NOT '; } print "$line \tdoes $not match the regexp.\n"; } __DATA__ word1 is made of word2 word1, which is an important thing because of its use and stuff, is made of word2. #### word1 is made of word2 does match the regexp. word1, which is an important thing because of its use and stuff, is made of word2. does NOT match the regexp.