Jump to content

Module:StringTools

From Wikipedia, the free encyclopedia
localp={}--[=[ 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]=]functionp.imatch(text,pattern,start,plain)locali=0localpos=startor0ifnotmw.ustring.find(pattern,"%b()")thenpattern="("..pattern..")"endreturnfunction()i=i+1localreturn_values={mw.ustring.find(text,pattern,pos,plain)}localj=return_values[2]if#return_values>0thenpos=j+1-- Skip the first two returned values, which are the indices of the-- whole match.returni,unpack(return_values,3)elsereturnnil,nilendendendreturnp
close