while (my ($word) = $sequences =~ /(\S+)/g) { if (index($word, $quart1) >= 0 and index($word, $quart2) >= 0) { print "Found both in $word\n"; } }