8

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") 
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Nyan Octo Cat
  • 159
  • 2
  • 4
  • 8

3 Answers3

22

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
sisoft
  • 953
  • 10
  • 22
5

An important point - solutions which use gmatch to drop the delimiter do NOT match empty strings between two newlines, so if you want to preserve these like a normal split implementation (for example, to compare lines across two documents) you are better off matching the delimiter such as in this example:

function string:split(delimiter)
  local result = { }
  local from  = 1
  local delim_from, delim_to = string.find( self, delimiter, from  )
  while delim_from do
    table.insert( result, string.sub( self, from , delim_from-1 ) )
    from  = delim_to + 1
    delim_from, delim_to = string.find( self, delimiter, from  )
  end
  table.insert( result, string.sub( self, from  ) )
  return result
end

Credit to https://gist.github.com/jaredallard/ddb152179831dd23b230.

Hugheth
  • 1,802
  • 17
  • 14
2

I found the answer: use "[^\r\n]+" ('+' is for skipping over empty lines).

Before, I was purposely avoiding using brackets because I thought that it indicates a special string literal that doesn't support escaping. Well, that was incorrect. It is double brackets that do that.
Lua string.gsub() by '%s' or '\n' pattern

NateS
  • 5,751
  • 4
  • 49
  • 59
Nyan Octo Cat
  • 159
  • 2
  • 4
  • 8