git @ Cat's Eye Technologies Dipple / master lua / findstart.lua
master

Tree @master (Download .tar.gz)

findstart.lua @masterraw · history · blame

-- SPDX-FileCopyrightText: Chris Pressey, the original author of this work, has dedicated it to the public domain.
-- For more information, please refer to <https://unlicense.org/>
-- SPDX-License-Identifier: Unlicense

--
-- In a Lua pattern, "^" matches the start of a string, but
-- string.find also takes an index for where to start searching.
--
-- If you specify both, which one wins?
--

text = "12345 oh, a string with five (i.e. 6 minus 1) leading digits."

function findsub(text, pattern, index)
    local a, b, sub
    a, b = string.find(text, pattern, index)
    if a ~= nil then sub = "'" .. text:sub(a, b) .. "'" else sub = "" end
    print(pattern, index, ":", a, b, sub)
end

findsub(text, "%d+")
findsub(text, "%d+", 3)
findsub(text, "%d+", 10)

findsub(text, "^%d+")
findsub(text, "^%d+", 3)
findsub(text, "^%d+", 10)