I want to separate a string into an array of words and punctuation at once, in the same order with :gsub . I don’t think there’s a way to pass a logical expression to a :gsub and make it choose either of two patterns, so I don’t know how to approach this.
Below is the theoretical form of what I’m trying to achieve:
local str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit..."
local t = {}
str:gsub(
"%a+" or "%p+", -- Somehow split into words or punctuation
function(c) table.insert(t,c) end
)
for _,v in ipairs (t) do print(v) end
Expected:
Lorem
ipsum
dolor
sit
amet
,
consectetur
adipiscing
elit
...
Although I found out it’s impossible to do alternations with Lua patterns, I solved it with a little manual trimming. This works as expected:
local str = "Lorem ipsum dolor sit amet, consectetur-adipiscing elit..."
local t = {}
while true do
local first = str:match("^%w+") or str:match("^%p+")
if not first then break end
table.insert(t,first)
local trimmed = str:gsub(first,"",1)
str = trimmed:gsub("^%s+","",1)
end
for _,v in ipairs (t) do print(v) end