in reply to Re: force regular expression to match every item only once
in thread force regular expression to match every item only once

The main idea is to get every pair to get matched only once BUT if there are two pairs identical in the keyword and in the dictionary there are 2 identical keywords too, then every keyword will get matched once..

I will try to elaborate:
keyword:zezlze --> ze ez zl lz ze
dicWord:zezezd --> ze ez ze ez zd

so the score of this part should be 3 as the ze appears twice and each one is matched with the corresponding and the ez is matched once ONLY as second ez the dictionary word hasn't found another match in the keyword

I hope that I explained well, if not, Please let me know.

Thank you very much for your help
  • Comment on Re^2: force regular expression to match every item only once

Replies are listed 'Best First'.
Re^3: force regular expression to match every item only once
by mscharrer (Hermit) on Apr 21, 2008 at 07:10 UTC
    Based on my last code, you could add a hash with the number of occurrence of every pair and then check if this pair was already matched this often:
    use strict; use warnings; my $word = "zezlze"; my $dictword = "zezezd"; my @pairs = $word =~ /(?=(..))/g; my %count; $count{$_}++ foreach (@pairs); my $matcher = qr/(?=(@{[join "|", @pairs]}))/; my %seen; my $matches = 0; my @matches; foreach my $match ($dictword =~ /$matcher/g) { if ($seen{$match}++ < $count{$match}) { $matches ++; push @matches, $match; } } print "Matches: @matches\n"; print "$matches $dictword \n";
    results in
    Matches: ze ez ze 3 zezezd