lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Here is a more compete answer.

$ cat try.lua

a = "hello, there!"

function words(s)
  local wl = {}
  s = gsub(s, "(%p+)", " %1 ") -- space out punctuation
  gsub(s, "(%S+)", function(w) tinsert(%wl, w) end)
  return wl
end

w = words(a)

foreachi(w,print)

$ lua try.lua
1       hello
2       ,
3       there
4       !

- Peter Shook

> "Hello, world!" into ["Hello", ",", "world", "!"] -- for what I'd use a 
> pattern like "\w+|[^\w\s]+".
> Any suggestions about how to implement it?