use strict; use warnings; my $input = 'foo bar baz'; my @test_strings = ( 'fooo bar baz', 'foo bar baaz', 'foo bbar baz', 'ffoo baar baz', # Matches: Should it? 'fxo bar baz', # No match: Different character, not dup. 'foo baaar baz' # No match: Too many 'a's! ); my $fuzziness = 1; # Let's try out the function for (@test_strings){ if (fuzzy_match($input, $_, $fuzziness)){ print "MATCHED: $_\n"; } else{ print "NO MATCH: $_\n"; } } sub fuzzy_match{ my ($input, $test_string, $fuzziness) = @_; # Build a regex from the input string my $regex = ''; for (split //, $input){ $regex .= quotemeta($_) . "\{1," . ($fuzziness + 1) . "}?"; } $test_string =~ m/$regex/; }