1

I'm trying write some code that looks at two data sets and matches them (if match), at the moment I am using string.find and this kinda work but its very rigid. For example: it works on check1 but not on check2/3, as theres a space in the feed or some other word. i like to return a match on all 3 of them but how can i do that? (match by more than 4 characters, maybe?)

check1 = 'jan'
check2 = 'janAnd'
check3 = 'jan kevin'

input = 'jan is friends with kevin'

if string.find(input.. "" , check1 )  then
print("match on jan")
end

if string.find( input.. "" , check2 )  then
print("match on jan and")
end

if string.find( input.. "" , check3 )  then
print("match on jan kevin")
end

PS: i have tried gfind, gmatch, match, but no luck with them

mmkevind
  • 76
  • 7

2 Answers2

1

find only does direct match, so if the string you are searching is not a substring you are searching in (with some pattern processing for character sets and special characters), you get no match.

If you are interested in matching those strings you listed in the example, you need to look at fuzzy search. This SO answer may help as well as this one. I've implemented the algorithm listed in the second example, but got better results with two- and tri-gram matching based on this algorithm.

Community
  • 1
  • 1
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
0

Lua's string.find works not just with exact strings but with patterns as well. But the syntax is a bit different from what you have in your "checks". You'd want check2 to be "jan.+" to match "jan" followed by one or more characters. Your third check will need to be jan.+kevin. Here the dot stands for any character, while the following plus sign indicates that this might be a sequence of one or more characters. There's more info at http://www.lua.org/pil/20.2.html.

uanfala
  • 11
  • 1