The string is like this: TEMPLATES="!$TEMPLATE templatename manufacturer model mode\n$TEMPLATE MacQuantum Wash Basic\n$$MANUFACTURER Martin\n$$MODELNAME Mac Quantum Wash\n$$MODENAME Basic\n"
My way to get strings without tags is:
local sentence=""
for word in string.gmatch(line,"%S+") do
if word ~= tag then
sentence=sentence .. word.." "
end
end
table.insert(tagValues, sentence)
E(tag .." --> "..sentence)
And I get output:
$$MANUFACTURER --> Martin
$$MODELNAME --> Mac Quantum Wash
...
...
But this is not the way I like. I would like to find first the block starting with $TEMPLATE tag to check if this is the right block. There is many such blocks in a file I read line by line. Then I have to get all tags marked with double $: $$MODELNAME etc. I have tried it on many ways, but none satisfied me. Perhaps someone has an idea how to solve it?
We are going to use Lua patterns (like regex, but different) inside a function string.gmatch, which creates a loop. Explanation:
for match in string.gmatch(string, pattern) do print(match) endis an iterative function that will iterate over every instance ofpatterninstring. The pattern I will use is%$+%w+%s[^\n]+%$+- At least 1 literal $ ($ is a special character so it needs the % to escape), + means 1 or more. You could match for just one ("%$") if you only need the data of the tag but we want information on how many $ there are so we'll leave that in.%w+- match any alphanumeric character, as many as appear in a row.%s- match a single space character[^\n]+- match anything that isn't '\n' (^ means invert), as many as appear in a row. Once the function hits a \n, it executes the loop on the match and repeats the process.That leaves us with strings like "$TEMPLATE templatename manufacturer" We want to extract the $TEMPLATE to its own variable to verify it, so we use
string.match(string, pattern)to just return the value found by the pattern in string.OK: EDIT: Here's a comprehensive example that should provide everything you're looking for.
Output: