Difference between revisions of "Module:String"

Jump to navigation Jump to search
278 bytes added ,  07:56, 17 August 2020
remove seemingly unnecessary letter
>Yurik
(updated from enwiki)
>Amire80
(remove seemingly unnecessary letter)
 
(4 intermediate revisions by 3 users not shown)
Line 113: Line 113:


Usage:
Usage:
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch}}
OR
OR
{{#invoke:String|pos|s=source_string|pattern=pattern_string|start=start_index
{{#invoke:String|pos|s=source_string|pattern=pattern_string|start=start_index
Line 143: Line 143:
For information on constructing Lua patterns, a form of [regular expression], see:
For information on constructing Lua patterns, a form of [regular expression], see:


* http://www.lua.org/manual/5.1/manual.html#5.4.1
* https://www.lua.org/manual/5.1/manual.html#5.4.1
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
* https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
* https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns


]]
]]
function str.match( frame )
-- This sub-routine is exported for use in other modules
local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} );
function str._match( s, pattern, start, 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
if s == '' then
return str._error( 'Target string is empty' );
return str._error( 'Target string is empty' );
Line 163: Line 156:
return str._error( 'Pattern string is empty' );
return str._error( 'Pattern string is empty' );
end
end
start = tonumber(start) or 1
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
return str._error( 'Requested start is out of range' );
return str._error( 'Requested start is out of range' );
Line 214: Line 208:
return result;
return result;
end
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
end


Anonymous user

Navigation menu