Content deleted Content added
Dinoguy1000 (talk | contribs) copy fixed comment from sandbox: this has been wrong for years, and when I corrected it years ago, it just got overwritten later, so rather tan see it happen again, I'm just making the change in the live module now |
Fix |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 59:
Parameters
s: The string to return a subset of
i: The
j: The last index of the string to return, defaults to the last character.
Line 107:
--[[
_match
match▼
This function returns a substring from the source string that matches a
specified pattern. It is exported for use in other modules
Usage:
strmatch = require("Module:String")._match
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}▼
sresult = strmatch( s, pattern, start, match, plain, nomatch )
OR▼
{{#invoke:String|match|s=source_string|pattern=pattern_string|start=start_index▼
|match=match_number|plain=plain_flag|nomatch=nomatch_output}}▼
Parameters
Line 131 ⟶ 129:
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▼
trailing whitespace from each string. In some circumstances this is desirable, in▼
other cases one may want to preserve the whitespace.▼
If the match_number or start_index are out of range for the string being queried, then▼
this function generates an error. An error is also generated if no match is found.▼
If one adds the parameter ignore_errors=true, then the error will be suppressed and▼
an empty string will be returned on any failure.▼
For information on constructing Lua patterns, a form of [regular expression], see:
Line 209 ⟶ 198:
end
end
--[[
▲match
This function returns a substring from the source string that matches a
specified pattern.
Usage:
▲{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
▲OR
▲{{#invoke:String|match|s=source_string|pattern=pattern_string|start=start_index
▲ |match=match_number|plain=plain_flag|nomatch=nomatch_output}}
Parameters
s: The string to search
pattern: The pattern or string to find within the string
start: The index within the source string to start the search. The first
character of the string has index 1. Defaults to 1.
match: In some cases it may be possible to make multiple matches on a single
string. This specifies which match to return, where the first match is
match= 1. If a negative number is specified then a match is returned
counting from the last match. Hence match = -1 is the same as requesting
the last match. Defaults to 1.
plain: A flag indicating that the pattern should be understood as plain
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
▲trailing whitespace from each string. In some circumstances this is desirable, in
▲other cases one may want to preserve the whitespace.
▲If the match_number or start_index are out of range for the string being queried, then
▲this function generates an error. An error is also generated if no match is found.
▲If one adds the parameter ignore_errors=true, then the error will be suppressed and
▲an empty string will be returned on any failure.
For information on constructing Lua patterns, a form of [regular expression], see:
* http://www.lua.org/manual/5.1/manual.html#5.4.1
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
]]
-- This is the entry point for #invoke:String|match
function str.match( frame )
Line 375 ⟶ 407:
if plain then
pattern = str._escapePattern( pattern )
replace =
end
Line 480 ⟶ 512:
end
-- findpagetext returns the position of a piece of text in a page
-- First positional parameter or |text is the search text
-- Optional parameter |title is the page title, defaults to current page
-- Optional parameter |plain is either true for plain search (default) or false for Lua pattern search
-- Optional parameter |nomatch is the return value when no match is found; default is nil
function str._findpagetext(args)
-- process parameters
local nomatch = args.nomatch or ""
if nomatch == "" then nomatch = nil end
--
local text = mw.text.trim(args[1] or args.text or "")
if text == "" then return nil end
--
local title = args.title or ""
local titleobj
if title == "" then
titleobj = mw.title.getCurrentTitle()
else
titleobj = mw.title.new(title)
end
--
local plain = args.plain or ""
if plain:sub(1, 1) == "f" then plain = false else plain = true end
-- get the page content and look for 'text' - return position or nomatch
local content = titleobj and titleobj:getContent()
return content and mw.ustring.find(content, text, 1, plain) or nomatch
end
function str.findpagetext(frame)
local args = frame.args
local pargs = frame:getParent().args
for k, v in pairs(pargs) do
args[k] = v
end
if not (args[1] or args.text) then return nil end
-- just the first value
return (str._findpagetext(args))
end
--[[
Helper function that populates the argument list given that user may need to use a mix of
Line 551 ⟶ 620:
]]
function str._escapePattern( pattern_str )
return
end
|