You should use string.match instead. Which will return the string which matches our pattern, then we can just get the length of it using the # operator
local function SimplifyStrings(str)
return #str:match("#%(\"(.+)\"%)$")
end
print(SimplifyStrings("#(\"does it work?\")")) -- 13
print(SimplifyStrings("#(\"string\")")) -- 6
I’m not sure if this is exactly what you wanted, and im also not the best at string patterns so there is probably a more elegant solution
But here is what I got…
local function SimplifyStrings(str)
local sum = "";
for w in str:gmatch("[^#%(\"(.+)\"%)]+") do
sum ..= string.len(w) .. " "
end
return sum
end
print(SimplifyStrings("#(\"string\")#(\"does it work?\")")) -- 6 13
local function SimplifyStrings(String)
for capture in string.gmatch(String, "#%(\"([^\"]+)") do
String = String:gsub(
"#%(\"([^\"]+)\"%)",
#capture,
1
)
end
return String
end
print(SimplifyStrings(
"#(\"Did it work?\") some strings in between #(\"Yes it did!\")"
))