my $first = "the"; my $last = "dog"; my $string = "The black dog danced around the sleeping dog." my @matches = $string =~ m/\b($first\b(?!.*?$first.*?)\b$last)\b/g; #### my $first = "the"; my $last = "dog"; my @strings = ("The black dog danced around the sleeping dog.", "The brown bear leaped over the lazy dog."); for my $string (@strings) { my @match = $string =~ m/(?=\b($first\b.*?\b$last)\b)/gi; for my $match (@match) { my @firsts = $match =~ m/\b($first)\b/gi; my @lasts = $match =~ m/\b($last)\b/gi; if ((@firsts == 1) and (@lasts == 1)) { print "$match\n"; } } } # The black dog # the sleeping dog # the lazy dog