I am trying to gsplit my text into multiple tables using a pattern.
So this is my input.
\x10Hello\x0AWorld
This is what I expect in my output,
\x0A
<- similar inputs will always be 4 chars long
{{'\x10', 'Hello'}, {'\x0A', 'World'}}
This is what I have tried so far.
local function splitIntoTable(input)
local output = {}
for code, text in (input):gmatch('(\\x%x+)(.*)') do
print(code .. ' ' .. text);
table.insert(output, { code, text })
end
return output
end
I made 2 regex groups in gmatch
the first group is for the hex and the second group is for the text, I am not sure why this isn't working. The print
statement never gets executed so the loop is never being used.
The pattern '\\x%x+'
matches a literal backslash, an x, and a sequence of hex digits. It does not match the ASCII character generated by a hexadecimal escape such as '\x0A'
.
You need to replace it with a character class in square brackets such as '[\x10\x0A]'
. You will have to fill in the character class with whatever ASCII characters (or other bytes) you are expecting in that position in the match.
Unfortunately, this pattern will only match once in a string like '\x10Hello\x0AWorld'
. The second part of the pattern also needs to be modified.
local function splitIntoTable(input)
local output = {}
for code, text in (input):gmatch('([\x10\x0A])(.*)') do
print(code .. ' ' .. text);
table.insert(output, { code, text })
end
return output
end