Reputation: 63
Lets say I have the following error string:
err = "/mnt/cd4/autorun.lua:43: 'end' expected (to close 'while' at line 1) near '-eof-'"
How would I parse the file path, line number, and the error message separately from the string?
I have no prior experience in parsing Lua strings, so I thought asking here would be useful. I also tried finding a topic solving the same matter but I could not find one.
Upvotes: 4
Views: 644
Reputation: 26744
Something like this should work:
err = "/mnt/cd4/autorun.lua:43: 'end' expected (to close 'while' at line 1) near '-eof-'"
local file, line, errmsg = err:match('^(.-):(%d+):(.+)')
print(file, line, errmsg)
The pattern says: capture starting at the end of the line (^
) a shortest group of zero or more (-
) of any symbol (.
), followed by :
, then a group of one or more digits (%d+
), followed by :
, and then a group of one of more symbols (.+
). You can read about patterns here.
Upvotes: 2