6.13. Approximate MatchingProblemYou want to match something fuzzily. Any time you want to be forgiving of misspellings in user input, you want to do fuzzy matching. SolutionUse the String::Approx module, available from CPAN: use String::Approx qw(amatch); if (amatch("PATTERN", @list)) { # matched } @matches = amatch("PATTERN", @list); Discussion
String::Approx calculates the difference between the pattern and each string in the list. If less than a certain number (by default, 10 percent of the length of the pattern) one-character insertions, deletions, or substitutions are required to make the string from the pattern, the string "matches" the pattern. In scalar context, use String::Approx qw(amatch); open(DICT, "/usr/dict/words") or die "Can't open dict: $!"; while(<DICT>) { print if amatch("balast"); }
You can also pass options to It must be noted that using the module's matching function seems to run between 10 and 40 times slower than Perl's built-in matching function. Only use String::Approx if you're after fuzziness in your matching that Perl's regular expressions can't provide. See AlsoThe documentation for the CPAN module String::Approx; Recipe 1.16 |
|