lua-users home
lua-l archive

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



On 2-Jul-07, at 4:52 PM, RJP Computing wrote:

Is there a way to loop through a string "lines" at a time? Kind of like file:lines().

If you don't care about losing empty lines, the easiest way is:

  for line in str:gmatch"[^\n]+" do ... end

(This also might leave a trailing \r on the end of line, if
your input was created with them.)

If you only wanted lines which started with the word "Project",
you could use:

  for line in str:gmatch"Project[^\n]*" do ... end

etc.

It's not clear to me why you have the literal string gtxAdvanced in
your example pattern, but even if you fixed that, the .* on either
side of it are greedy, so the pattern is not going to do what you want.
If possible, it's preferable to use negative character spans (like
[^{}]*) rather than non-greedy .-, but sometimes .- is a lot easier.