}}
{{archives|banner=yes|age=90|bot=lowercase sigmabot III}}
== Replace ==
Hello all, I ran into a problem and would appreciate any help. Trying to use this function to replace strings with <code><nowiki>[[</nowiki></code> does not work, I guess because it tries to parse as a link. Example:
*Trying to transform two words separate by <code> - </code> into two wikilinks:
<code><nowiki>[[{{subst:#invoke:String|replace|Foo-bar|-|]] and [[}}]]</nowiki></code>
*As you can see, it does not work. Replacing with the HTML code <code>&#91;</code> is not a option because it does not create wikilinks.
I understand one could "post-process" that output, like replacing <code>&#91;</code> with <code> [ </code> using {{tlx|Str rep}} but that's too ugly :) Is there a way to circumvent that? I'm missing something? [[User:Cainamarques|Cainamarques]] ([[User talk:Cainamarques|talk]]) 14:00, 3 December 2013 (UTC)
: Your problem is that MediaWiki considers the brackets as well as the braces when trying to parse the wikitext, so it sees it as two potential links ("<code><nowiki>[[{{subst:#invoke:String|replace|Foo-bar|-|]]</nowiki></code>" and "<code><nowiki>[[}}]]</nowiki></code>") rather than as one parser function call inside brackets that has more brackets in its arguments. There doesn't seem to be any easy way around this. [[User:Anomie|Anomie]][[User talk:Anomie|⚔]] 14:57, 3 December 2013 (UTC)
* Just curious here, what exactly are you trying to accomplish? Maybe there is a way to achieve your goal without replacing <code>-</code> with <code><nowiki>]] and [[</nowiki></code>? [[User:Technical 13|Technical 13]] ([[User talk:Technical 13|talk]]) 15:02, 3 December 2013 (UTC)
'''I had lunch, thought a little bit and found a solution:'''
*<pre>[[{{subst:#invoke:String|replace|Foo-bar|-|]] and {{subst:User:Cainamarques/sandbox}}}}]]</pre>
where [[User:Cainamarques/sandbox]] just contain two brackets <code>[[</code> hehe.
{{U|Technical 13}}, definitely there is. All I need is to create wikilinks of two pages that are contained within a page title. These pages are separated by a semicolon and a whitespace, like:
*Wikipédia:Fusão/Central de fusões/Imagem 3D; Estereoscopia
In this example, the pages are "Imagem 3D" and "Estereoscopia". The code is gonna be in a Preload page, and it's necessary to output clean wikicode, so <code>subst:</code> is needed. So the code below is what I came up to:
*<pre>[[{{<includeonly>subst:</includeonly>#invoke:String|replace|{{#titleparts:{{PAGENAME}}||3}}|(; )|=]] [[|plain=false}}]]</pre>
I guess it's ok. Sorry for my english. [[User:Cainamarques|Cainamarques]] ([[User talk:Cainamarques|talk]]) 15:48, 3 December 2013 (UTC)
* {{U|Cainamarques|Cain}}, you ''are'' aware that [[WP:Lua|Lua]] '''Module:'''s are not [[WP:SUBST|subst:]]<nowiki />itutable, right? [[User:Technical 13|Technical 13]] ([[User talk:Technical 13|talk]]) 16:07, 3 December 2013 (UTC)
** I am now :) Makes sense now that I think about it... Thank you, {{U|Technical 13}}. [[User:Cainamarques|Cainamarques]] ([[User talk:Cainamarques|talk]]) 16:24, 3 December 2013 (UTC)
** {{ping|Technical 13}} Yes they are. Look at [[Module:Unsubst]] for a prime example. [[User:Anomie|Anomie]][[User talk:Anomie|⚔]] 16:33, 3 December 2013 (UTC)
* {{U|Cainamarques|Cain}}, I'm guessing by the "Wikipédia:Fusão/Central de fusões/Imagem 3D; Estereoscopia" pagename that this is not on the English Wikipedia here, can you check [[Special:Version]] of the the Wikipedia where you want to do this and see if [[mw:Extension:StringFunctions]] or something similar is installed? If it is, I may be able to help you with an alternative that will be [[WP:SUBST|subst:]]<nowiki />itutable. [[User:Technical 13|Technical 13]] ([[User talk:Technical 13|talk]]) 16:36, 3 December 2013 (UTC)
Anomie is right, modules are substitutable. Anyway my solution above stops working when the simple text "Foo-bar" is changed to the expression I wanna use: <code><nowiki>{{ #titleparts:{{PAGENAME}} }}</nowiki></code>. Even so, it would work if not trying to <code>subst:</code> the Lua module. Either way, it is not good enough. If not for [[bugzilla:2777]], it would be easy as pie. {{U|Technical 13}}, I'm from pt.wiki, there is no such thing, thank you for the support. I'll try again in the future... [[User:Cainamarques|Cainamarques]] ([[User talk:Cainamarques|talk]]) 17:52, 3 December 2013 (UTC)
=== Another ===
:<code><nowiki>{{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;"><!-- 1em/8 : equivalent to a "fine space" -->!</span> |pattern= %b<> }}}}</nowiki></code> → {{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;"><!-- 1em/8 : equivalent to a "fine space" -->!</span> |pattern= %b<> }}}}
:<code><nowiki>{{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;">!</span> |pattern= %b<> }}}}</nowiki></code> → {{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;">!</span> |pattern= %b<> }}}}
:<code><nowiki>{{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left:.125em;">!</span> |pattern= %b<> }}}}</nowiki></code> → {{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left:.125em;">!</span> |pattern= %b<> }}}}
The same when using <code><nowiki>pattern=<.-></nowiki></code>:
:<code><nowiki>{{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;"><!-- 1em/8 : equivalent to a "fine space" -->!</span> |pattern= <.-> }}}}</nowiki></code> → {{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;"><!-- 1em/8 : equivalent to a "fine space" -->!</span> |pattern= <.-> }}}}
:<code><nowiki>{{#invoke:String|len|s={{#invoke:String|replace|source= {{#tag:nowiki|<span style="padding-left: 0.125em;"><!-- 1em/8 : equivalent to a "fine space" -->!</span>}} |pattern= %b<> }}}}</nowiki></code> → {{#invoke:String|len|s={{#invoke:String|replace|source= {{#tag:nowiki|<span style="padding-left: 0.125em;"><!-- 1em/8 : equivalent to a "fine space" -->!</span>}} |pattern= %b<> }}}}
What to do? I am trying to get a result of "1". --[[User:Jerome Charles Potts|Jerome Potts]] ([[User talk:Jerome Charles Potts|talk]]) 19:00, 28 May 2014 (UTC)
:The string module defaults to <code>|plain=true</code> which means the search pattern is plain text and is ''not'' a regular expression. To fix, add <code>|plain=false</code> in the above. [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 02:38, 29 May 2014 (UTC)
::Oops ! Thank you much. --[[User:Jerome Charles Potts|Jerome Potts]] ([[User talk:Jerome Charles Potts|talk]]) 05:05, 29 May 2014 (UTC)
== "Replace" behaves differently when I name parameters vs. when I don't ==
In [[Template:AfC comment/sandbox]] if I specify <code>source=</code> or <code>pattern=</code> the results seen in [[Template:AfC comment/testcases]] behave differently, despite the documentation implying that behavior should be the same regardless of whether these parameters are named or left unnamed. Why is that? [[User:Wbm1058|wbm1058]] ([[User talk:Wbm1058|talk]]) 17:14, 4 February 2021 (UTC)
:
: {{re|Wbm1058}} When you use named parameters in any template, any whitespace is trimmed from the start and end of the parameter value. So named parameters will not "see" newlines at the start or end of the {{para|source}} parameter. Here's how it works in [[Template:AfC comment]] and [[Template:AfC comment/sandbox]] (I've shown <nowiki>{{tl|paragraph break}}</nowiki> so you can see where it would appear):
<div style="border:1px solid grey; margin-left:1.7em; padding:0.5em; background-color:#EFE;>
Positional parameters:
<syntaxhighlight lang="wikitext">
{{#invoke:String|replace|
|
+|{{tl|paragraph break}}||false}}
</syntaxhighlight>
Result:
{{#invoke:String|replace|
|
+|{{tl|paragraph break}}||false}}
Named parameters:
<syntaxhighlight lang="wikitext">
{{#invoke:String|replace|source=
|pattern=
+|replace={{tl|paragraph break}}|count=|plain=false}}
</syntaxhighlight>
Result:
{{#invoke:String|replace|source=
|pattern=
+|replace={{tl|paragraph break}}|count=|plain=false}}
</div>
: Hope that helps. --[[User:RexxS|RexxS]] ([[User talk:RexxS|talk]]) 17:52, 4 February 2021 (UTC)
== Function to find in a substring? ==
I have a bit of a complex problem to solve.
My first attempt to implement this code in a template:
<syntaxhighlight lang="wikitext">
{{#ifexpr: {{#invoke:string|find|{{:{{SUBJECTPAGENAME}}}}|<search string>}}|<string found>|<string not found>}}}}
</syntaxhighlight>
caused a template loop because SUBJECTPAGENAME transcludes the template. But I expect the recursive transclusion to be towards the end of SUBJECTPAGENAME and the string I'm searching for to be towards the beginning. So my second attempt is to search just the first 500 characters, to avoid triggering the template loop:
<syntaxhighlight lang="wikitext">
{{#ifexpr: {{#invoke:string|find|{{#invoke:string|sub|{{:{{SUBJECTPAGENAME}}}}|1|500}}|<search string>}}|<string found>|<string not found>}}}}
</syntaxhighlight>
This works if SUBJECTPAGENAME is replaced with the actual subject page name but fails with this error when I try substituting the {<nowiki/>{#invoke:string|sub:
:<strong class="error">String Module Error: String subset index out of range</strong>
I assume the index (500) is out of range because it's trying to search the raw code <code>{<nowiki/>{:{<nowiki/>{SUBJECTPAGENAME}}}}</code> rather than the transcluded page because the page is >500 characters long and the template loop call is located after the 500th character.
<syntaxhighlight lang="wikitext">
{{#invoke:string|sub|{{:{{SUBJECTPAGENAME}}}}|1|500}}
</syntaxhighlight>
works fine when it stands alone. The index out-of-range error only happens when this is nested inside of the <code>{<nowiki/>{#invoke:string|find</code> so I assume this syntax is just too complex.
Is there an <code>{<nowiki/>{#invoke:string|find-in-sub|</code> function that combines the functions of |find| and |sub| so I don't have to nest these? If not, can someone write one for me? Thanks. – [[User:Wbm1058|wbm1058]] ([[User talk:Wbm1058|talk]]) 15:02, 18 February 2022 (UTC)
: '''|find|''' has a '''start''' parameter: The index within the source string to start the search, defaults to 1
: Maybe all I need is an '''stop''' parameter: The index within the source string to stop the search, defaults to end-of-string – [[User:Wbm1058|wbm1058]] ([[User talk:Wbm1058|talk]]) 15:17, 18 February 2022 (UTC)
: {{ec}} This is an [[XY problem]]. Try using [[Module:Page]] to get the raw wikitext of the page ({{mlx|page|getContent|<nowiki>{{SUBJECTPAGENAME}}</nowiki>}as{{=}}raw}}), and then doing whatever string magic you are trying to do. That said, if I had to guess the direct problem you are experiencing is that you need to subst <code><nowiki>{{SUBPAGENAME}}</nowiki></code> as well, or otherwise it doesn't get expanded when you try to substitute the string call. [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 15:18, 18 February 2022 (UTC)
:: Thanks {{U|Pppery}}. I wasn't aware of that function to get the raw wikitext. That gets me yet teasingly closer to a solution that works. Getting the raw wikitext is I suppose a cleaner way to avoid the template loop. Now there are no errors when I substitute. But, the problem is that I need to substitute, and my magic is only a viable solution if it works with only transclusion. My specific problem is demonstrated at {{no redirect|Talk:David Ayers}}. That is populating [[:Category:Articles with talk page redirects]]. But if I {{tls|R from move/except/sandbox}} it populates [[:Category:Redirects for discussion with talk page redirects]], the new category my code is intended to implement. I need that category populated with transclusion, not just substitution. I believe my proposed solution above might do that. – [[User:Wbm1058|wbm1058]] ([[User talk:Wbm1058|talk]]) 16:06, 18 February 2022 (UTC)
::: Does [[Special:Diff/1072625351]] fix the problem? [[User:Pppery|* Pppery *]] [[User talk:Pppery|<sub style="color:#800000">it has begun...</sub>]] 16:57, 18 February 2022 (UTC)
:::: Indeed that does! Doh. I initially did try searching for <code><nowiki>#invoke:RfD</nowiki></code> but wasn't finding it because of the transclusion! And <code>{<nowiki/>{#invoke:page|getContent|{<nowiki/>{SUBJECTPAGENAME}}|as=raw}}</code> was the perfect solution for that, rather than the dead-end path I tried. I've been working on this on-and-off for the past day, so very happy to finally have a solution. Thanks! [[User:Wbm1058|wbm1058]] ([[User talk:Wbm1058|talk]]) 17:55, 18 February 2022 (UTC)
== Extract number ==
|