|
One case it doesn't handle is the mix of ' and " in the string (as the
string should only be closed with the same quote is was opened with,
assuming both " and ' are allowed).
This code should handle this case (also added examples):
local list = {
'123,"ABC, DEV 23",345,,202,NAME',
',"ABC, DEV 23",345,534,202,NAME',
'123,"ABC, DEV 23",345,534.202,',
'123 , "ABC, DEV 23" , 345 , 534 , 202 , NAME',
[[123,"ABC, \"he,llo\", DEV 23",,,,NAME]],
[[123,"ABC, \\",llo\", DEV 23,,,,NAME]],
[[123,'ABC, "hel,lo", DEV 23',342,534,202,NAME]],
[[123,"ABC, 'hel,lo, DEV 23",342,534,202,NAME]],
}
local escaped, instring, pos, start = false, false, -1, ""
for _,s in ipairs(list) do
s = s:gsub([[()(["',\\])]], function(p, s)
if p > pos+1 then escaped = false end
if s == '\\' then escaped, pos = not escaped, p end
if (s == '"' or s == "'") and not escaped
and (not instring or s == start) then instring, start = not
instring, s end
if s == ',' and not instring then s = '~' end
return s
end)
print(s)
end