local x = "Mr %gra-b"
local y = "Mr %gra-b is your master-!"
y = y:match(x)
print(y) --expecting Mr %gra-b
It's printing nil sadly. Removing special characters will make it work. But we want the string to return as it is.
local x = "Mr %%gra%-b"
local y = "Mr %gra-b is your master-!"
y = y:match(x)
print(y) --expecting Mr %gra-b
Magic characters ^$()%.[]*+-?
need to be escaped by prepending %
! Please read the Lua manual!
Yes, but what if local x is a user input? We have to assume the user does not know magic characters. For example. a filter search of names and words?
What does it matter if the text is provided in a variable or through user input. The solution is still the same. You need to escape magic characters.
x = x:gsub("%W", "%%%0")
You prepend a %
to any magic character. Again, read the Lua manual!
Alternatively use Egor's suggestion:
y:find(x, 1, true)
the third argument will suppress pattern matching and simply search the provided string. If you only want to check wether the string is present in your string this is probably the simplest solution