Split a string by \n or \r using string.gmatch()

Nyan Octo Cat picture Nyan Octo Cat · Sep 29, 2015 · Viewed 11k times · Source

A simple pattern should do the job but I can't come up with/find something that works. I am looking to have something like this:

lines = string.gmatch(string, "^\r\n") 

Answer

sisoft picture sisoft · Sep 29, 2015

To split a string into table (array) you can use something like this:

str = "qwe\nasd\rzxc"
lines = {}
for s in str:gmatch("[^\r\n]+") do
    table.insert(lines, s)
end