metis.string.fuzzy
Determines if an input string str
matches a given search pattern
.
This does not do a direct sub-string check (like string.find
), but instead
checks if the input string fuzzily (or approximately) matches the pattern.
If the string matches, this returns a score of how well the input string matches the pattern. A string is considered to match the pattern if every letter pattern appears in order within the string.
For instance, the input "Cobblestone" is matched by the patterns "Cobblestone", "cbbl" and "cbst", in decreasing order of score. Similarly, the pattern "stn" matches "Stone", "Cobblestone" and "Stained Glass", again with decreasing scores.
Usage
Match "ComputerCraft" against a series of different patterns.
local fuzzy = require "metis.string.fuzzy" print(fuzzy("ComputerCraft", "Comp")) -- 15 print(fuzzy("ComputerCraft", "CC")) -- -7 print(fuzzy("ComputerCraft", "garbage")) -- nil
Parameters
Returns
number
| nil The "score" for this match (higher is better), ornil
if it did not match.