in reply to force regular expression to match every item only once
One better way would be to first match all patterns multiple times and then filter the list using a hash:
Here we iterate over the results and mark all matches as seen, but only count them the first time. In addition when you need the list of real matches you can push the first matches in an array like shown above.use strict; use warnings; my $word = "zezl"; my $dictword = "zezezl"; my @pairs = $word =~ /(?=(..))/g; my $matcher = qr/(?=(@{[join "|", @pairs]}))/; my %seen; my $matches = 0; my @matches; foreach my $match ($dictword =~ /$matcher/g) { if (not exists $seen{$match}) { $matches ++; push @matches, $match; } $seen{$match} = 1; } print "Matches: @matches\n"; print "$matches $dictword \n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: force regular expression to match every item only once
by hiddenOx (Novice) on Apr 21, 2008 at 02:49 UTC | |
by mscharrer (Hermit) on Apr 21, 2008 at 07:10 UTC |