lua-users home
lua-l archive

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


> I'm having a little trouble figuring out how patterns and gsub work. I 
> followed the example in the documentation to break a string up into a table 
> of words, but I need to do two things: first, just take the first 
> (space-delimited) word off the beginning of the string (this is for 
> splitting it up into command and argument strings). I also need to have 
> anything between a pair of ' be read as one word, for example the string 
> "first 'this is one word' third" would be split up into {"first", "this is 
> one word", "third"}. Can anyone tell me how to do this?
  
  line = gsub(line, "'(.-)'", function (l) return gsub(l, " ", "\1") end)
  tab = {}
  gsub(line, "(%S+)", function (l)
    l = gsub(l, "\1", " ")
    tinsert(tab, l)
  end)
  
  command = tremove(tab, 1)

-- Roberto