There may be ways to do this with smart parsing, but an alternative way would be to track a simple state and merge fragments based on the detection of quoted fragments. Something like this might work:
local text = [[I "am" 'the text' and "some more text with '" and "escaped \" text"]] local spat, epat, buf, quoted = [=[^(['"])]=], [=[(['"])$]=] for str in text:gmatch("%S+") do local squoted = str:match(spat) local equoted = str:match(epat) local escaped = str:match([=[(\*)['"]$]=]) if squoted and not quoted and not equoted then buf, quoted = str, squoted elseif buf and equoted == quoted and #escaped % 2 == 0 then str, buf, quoted = buf .. ' ' .. str, nil, nil elseif buf then buf = buf .. ' ' .. str end if not buf then print((str:gsub(spat,""):gsub(epat,""))) end end if buf then print("Missing matching quote for "..buf) end
This will print:
I am the text and some more text with ' and escaped \" text
Updated to handle mixed and shielded quotes. Updated to remove quotes. Updated to handle quoted words.
source share