请输入您要查询的百科知识:

 

词条 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

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/12 3:26:26