Module:StringTools
local p = {}
--[=[
Similar to gmatch, but it returns the count of the match in addition to the
list of captures, something like ipairs().
If the pattern doesn't contain any captures, the whole match is returned.
Invoke thus:
for i, whole_match in require("Module:string").imatch(text, pattern) do
[ do something with i and whole_match ]
end
or
for i, capture1[, capture2[, capture3[, ...]]] in require("Module:string").imatch(text, pattern) do
[ do something with i and capture1 ]
end
]=]
function p.imatch(text, pattern, start, plain)
local i = 0
local pos = start or 0
if not mw.ustring.find(pattern, "%b()") then
pattern = "(" .. pattern .. ")"
end
return function()
i = i + 1
local return_values = { mw.ustring.find(text, pattern, pos, plain) }
local j = return_values[2]
if #return_values > 0 then
pos = j + 1
-- Skip the first two returned values, which are the indices of the
-- whole match.
return i, unpack(return_values, 3)
else
return nil, nil
end
end
end
return p
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.