The black dog danced around the sleeping dog.
...and the endpoints of "the" and "dog", it seems you want the minimal coverage. Here is where you need some test cases to demonstrate what you will and won't accept.
The way the regex engine works, if it starts to match, say on "the", it will exhaust all options before moving on the the next "the".
One example might be the string where the endpoints are not repeated inside the string. But the following doesn't work:
There doesn't seem to be a good way to say "I don't want $first anywhere in this part", except to do another match. Combine this with Athanasius's solution: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
-QM
--
Quantum Mechanics: The dreams stuff is made of
In reply to Re: Pattern matching: Lazy vs. greedy
by QM
in thread Pattern matching: Lazy vs. greedy
by false_friend
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |