[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: extension to string.gmatch (structural regular expressions)
- From: "Duncan Cross" <duncan.cross@...>
- Date: Thu, 3 Jan 2008 14:48:16 +0000
On Jan 3, 2008 11:50 AM, Shmuel Zeigerman <shmuz@actcom.co.il> wrote:
> Miles Bader wrote:
> >
> > Though in many cases, you don't really need split, e.g. something like
> > the following is actually cleaner than doing a split and iterating over
> > the resulting table:
> >
> > for field in string.gmatch (STRING, "[^ \t\n]+") do
> > .... use field ...
> > end
>
> And what if the fields are delimited by, say, "<delim>" ?
>
> The rule of thumb is to use gmatch when fields are easier to express
> with regexp than delimiters, otherwise use split (provided it's
> available :) ).
>
> --
> Shmuel
>
Here's a first attempt at a splitting gmatch:
function string.gmatchsplit(text, pattern)
local gm = text:gmatch("()" .. pattern .. "()")
local start = 1
local function aux(...)
if start == nil then
return nil
end
local caps = select('#', ...)
if caps == 0 then
local finalpart = text:sub(start)
start = nil
return finalpart
end
local stop = (...) - 1
local before = text:sub(start,stop)
start = select(caps, ...)
return before, select(2, ...)
end
return coroutine.wrap(
function()
while true do
coroutine.yield( aux(gm()) )
end
end
)
end
for part in string.gmatchsplit("one<delim>two<delim>three", "<delim>") do
print(part)
end