in reply to force regular expression to match every item only once
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
Here are two ways:
my @kw = sort qw( ze ez zl lz ze ); my @dw = sort qw( ze ez ze ez zd ); my $c = 0; my $kw_i = 0; my $dw_i = 0; while ($kw_i < @kw && $dw_i < @dw) { if ($kw[$kw_i] lt $dw[$dw_i]) { ++$kw_i; next; } if ($dw[$dw_i] lt $kw[$kw_i]) { ++$dw_i; next; } ++$c; ++$kw_i; ++$dw_i; }
my $dw = ' ' . join(' ', qw( ze ez ze ez zd )); my @kw = qw( ze ez zl lz ze ); my $c = 0; my $dw_ = $dw; for my $kw_part (@kw) { my $pos = index($dw_, " $kw_part"); next if $pos < 0; substr($dw_, $pos+1, 2, ''); ++c; }
Untested.
I made them non-destructive.
|
|---|