Regel 27: | Regel 27: | ||
for key, value in matches do | for key, value in matches do | ||
padded = padright( value, 5, '0' ) | padded = padright( value, 5, '0' ) | ||
+ | str:gsub( value, padded ) | ||
end | end | ||
− | -- | + | -- |
+ | return str | ||
end | end |
Versie van 10 aug 2023 09:50
Module:Pad numbers
Summary
Pad numbers in string.
Scriptfout: de module heeft de waarde nil teruggegeven. Dit moet een exporttabel zijn.
local p = {} local function padleft( str, len, char ) if char == nil then char = '0' end char = '0' res = string.rep( char, len - #str ) .. str return res end local function padright( str, len, char ) if char == nil then char = '0' end char = '0' res = str .. string.rep( char, len - #str ) return res end function p.main( frame ) str = mw.text.unstrip(frame.args[1]) padlength = mw.text.unstrip(frame.args[2]) regpattern = "!\d+!" matches = string.match( str, regpattern ) for key, value in matches do padded = padright( value, 5, '0' ) str:gsub( value, padded ) end -- return str end