lua-users home
lua-l archive

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


This is not a bad idea. In the past I've used the handy
position-capturing () to do similar things with the existing gmatch:

local start = 1
local text = "Hello World"
for stop, match, newstart in string.gmatch(text, "()(l)()") do
    local before = text:sub(start,stop-1)
    print(("%q"):format(before), ("%q"):format(match))
    start = newstart
end
local final = text:sub(start)
print(("%q"):format(final), nil)

It's possible that could be wrapped up in a smart gmatch-wrapping
function that adds "()(" and ")()" around the given pattern and passes
through results from a coroutine-based iterator of some kind, but it
probably wouldn't be very nice.