Module:String: Difference between revisions

m
50 revisions imported from mw:Module:String: importing String module as requested by User:Katsumi a.k.a. Upperdecker2562
(updated from enwiki)
m (50 revisions imported from mw:Module:String: importing String module as requested by User:Katsumi a.k.a. Upperdecker2562)
 
(5 intermediate revisions by 4 users not shown)
Line 113:
 
Usage:
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_outputnomatch}}
OR
{{#invoke:String|pos|s=source_string|pattern=pattern_string|start=start_index
Line 143:
For information on constructing Lua patterns, a form of [regular expression], see:
 
* httphttps://www.lua.org/manual/5.1/manual.html#5.4.1
* httphttps://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
* httphttps://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
 
]]
-- This sub-routine is exported for use in other modules
function str.match( frame )
local new_args =function str._getParameters_match( frame.args, {'s', 'pattern', 'start', 'match'match_index, 'plain', 'nomatch'} );
local s = new_args['s'] or '';
local start = tonumber( new_args['start'] ) or 1;
local plain_flag = str._getBoolean( new_args['plain'] or false );
local pattern = new_args['pattern'] or '';
local match_index = math.floor( tonumber(new_args['match']) or 1 );
local nomatch = new_args['nomatch'];
 
if s == '' then
return str._error( 'Target string is empty' );
Line 163 ⟶ 156:
return str._error( 'Pattern string is empty' );
end
start = tonumber(start) or 1
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
return str._error( 'Requested start is out of range' );
Line 214 ⟶ 208:
return result;
end
end
-- This is the entry point for #invoke:String|match
function str.match( frame )
local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} );
local s = new_args['s'] or '';
local start = tonumber( new_args['start'] ) or 1;
local plain_flag = str._getBoolean( new_args['plain'] or false );
local pattern = new_args['pattern'] or '';
local match_index = math.floor( tonumber(new_args['match']) or 1 );
local nomatch = new_args['nomatch'];
 
return str._match( s, pattern, start, match_index, plain, nomatch )
end