"ماڈیول:String" کے نسخوں کے درمیان فرق
adds updated match support with wider parameter choices, whitespace handling, etc. |
add nomatch option to str.match |
||
سطر 113: | سطر 113: | ||
Usage: | Usage: | ||
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag}} | {{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}} | ||
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 | ||
|match=match_number|plain=plain_flag}} | |match=match_number|plain=plain_flag|nomatch=nomatch_output}} | ||
Parameters | Parameters | ||
سطر 128: | سطر 128: | ||
counting from the last match. Hence match = -1 is the same as requesting | counting from the last match. Hence match = -1 is the same as requesting | ||
the last match. Defaults to 1. | the last match. Defaults to 1. | ||
plain: A flag indicating that the pattern should be understood as plain | |||
text. Defaults to false. | text. Defaults to false. | ||
nomatch: If no match is found, output the "nomatch" value rather than an error. | |||
If invoked using named parameters, Mediawiki will automatically remove any leading or | If invoked using named parameters, Mediawiki will automatically remove any leading or | ||
سطر 148: | سطر 149: | ||
]] | ]] | ||
function str.match( frame ) | function str.match( frame ) | ||
local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain'} ); | local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} ); | ||
local s = new_args['s'] or ''; | local s = new_args['s'] or ''; | ||
local start = tonumber( new_args['start'] ) or 1; | local start = tonumber( new_args['start'] ) or 1; | ||
سطر 154: | سطر 155: | ||
local pattern = new_args['pattern'] or ''; | local pattern = new_args['pattern'] or ''; | ||
local match_index = math.floor( tonumber(new_args['match']) or 1 ); | local match_index = math.floor( tonumber(new_args['match']) or 1 ); | ||
local nomatch = new_args['nomatch']; | |||
if s == '' then | if s == '' then | ||
سطر 204: | سطر 206: | ||
if result == nil then | if result == nil then | ||
return str._error( 'Match not found' ); | if nomatch == nil then | ||
return str._error( 'Match not found' ); | |||
else | |||
return nomatch; | |||
end | |||
else | else | ||
return result; | return result; |