in reply to Getting the shortest match?
As Abigail-II says, it would be expensive to get the shortest match, and the following code is expensive, but it does what you want for the examples.
Notice that it will return 'the quick yellow fox' in the last case since that's the first "shortest" match.use strict; use warnings; my $re = qr/the\b.*?quick\b.*?fox/; while (<DATA>) { chomp($_); my $text = $_; my $match; while ($text =~ /($re)/) { $match = $1; $text = substr($match, 1); } print "it matches '$match'\n"; } __DATA__ the dog and the bear are quick, but the quick white fox is quicker the quick fox and the brown fox the quick raven and the quick fox the quick yellow fox and the quick fox
Hope this helps, -gjb-
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Getting the shortest match?
by saintbrie (Scribe) on Jan 18, 2003 at 04:45 UTC |