Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua - How to find a substring with 1 or 2 characters discrepancy

Say I have a string

local a = "Hello universe"

I find the substring "universe" by

a:find("universe")

Now, suppose the string is

local a = "un#verse"

The string to be searched is universe; but the substring differs by a single character. So obviously Lua ignores it.

How do I make the function find the string even if there is a discrepancy by a single character?

like image 732
SatheeshJM Avatar asked Dec 10 '25 16:12

SatheeshJM


1 Answers

If you know where the character would be, use . instead of that character: a:find("un.verse")

However, it looks like you're looking for a fuzzy string search. It is out of a scope for a Lua string library. You may want to start with this article: http://ntz-develop.blogspot.com/2011/03/fuzzy-string-search.html

As for Lua fuzzy search implementations — I haven't used any, but googing "lua fuzzy search" gives a few results. Some are based on this paper: http://web.archive.org/web/20070518080535/http://www.heise.de/ct/english/97/04/386/

Try https://github.com/ajsher/luafuzzy.

like image 113
Alexander Gladysh Avatar answered Dec 12 '25 16:12

Alexander Gladysh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!