Crest wrote: ↑May 16th, 2024, 5:14 pm
Tested but unfortunately same result.
Took a little longer than expected, but this works for me:
Code: Select all
function Initialize()
tab = {some = 'Here is (something'}
end
function Esc(str)
--local esc = string.format('%q', str)
local esc = str:gsub('([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1')
return esc
end
function Output(key)
SKIN:Bang('!SetOption', 'Text', 'Text', tab[key])
SKIN:Bang('!Update')
end
function ShowText()
print(Esc(tab.some))
SKIN:Bang('!SetOption', 'TextMeasure', 'String', tab.some)
SKIN:Bang('!Update')
local txt = SKIN:GetMeasure('TextMeasure'):GetStringValue()
for line in txt:gmatch('[^\r\n]+') do
for key, val in pairs(tab) do
print(key, val, line)
if line:match(Esc(val)) then print('matched'); Output(key) else print('not matched') end
end
end
return 0
end
I let the
string.format('%q', ...) alternative there since it looks much simpler, but although it does escape "almost" everything as it should and
advertised, it doesn't produce the expected result even if I use
Esc(line) in the
if statement.
What I initially recommended works though, and as I believe it should happen, only the literal pattern (i.e.
val or
tab[some]) needs escaping, nothing else. There is a catch though - I tried escaping stuff like
\n or
\s after in the main string, and except literally using
\\n and
\\s there or declaring it as
tab = {some = [[Here is (something]]}, I got mixed results: it either didn't work (tried
local esc = str:gsub('([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1'):gsub('\\', '\\\\')), or it partially work and it kept the
\ but not the
n afterwards. You can use
" aka double quotes in the string without having to escape them, but not
' aka apostrophes / single quotes, though you can invert that if you want.