Jump to content

Module talk:String

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Sawol (talk | contribs) at 16:18, 30 August 2023 (MiszaBot). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Replace

Hello all, I ran into a problem and would appreciate any help. Trying to use this function to replace strings with [[ does not work, I guess because it tries to parse as a link. Example:

  • Trying to transform two words separate by - into two wikilinks:

[[{{subst:#invoke:String|replace|Foo-bar|-|]] and [[}}]]

  • As you can see, it does not work. Replacing with the HTML code [ is not a option because it does not create wikilinks.

I understand one could "post-process" that output, like replacing [ with [ using {{Str rep}} but that's too ugly :) Is there a way to circumvent that? I'm missing something? Cainamarques (talk) 14:00, 3 December 2013 (UTC)[reply]

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 ("[[{{subst:#invoke:String|replace|Foo-bar|-|]]" and "[[}}]]") 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. Anomie 14:57, 3 December 2013 (UTC)[reply]

I had lunch, thought a little bit and found a solution:

  • [[{{subst:#invoke:String|replace|Foo-bar|-|]] and {{subst:User:Cainamarques/sandbox}}}}]]

where User:Cainamarques/sandbox just contain two brackets [[ hehe.

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 subst: is needed. So the code below is what I came up to:

  • [[{{<includeonly>subst:</includeonly>#invoke:String|replace|{{#titleparts:{{PAGENAME}}||3}}|(; )|=]] [[|plain=false}}]]

I guess it's ok. Sorry for my english. Cainamarques (talk) 15:48, 3 December 2013 (UTC)[reply]

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: {{ #titleparts:{{PAGENAME}} }}. Even so, it would work if not trying to subst: the Lua module. Either way, it is not good enough. If not for bugzilla:2777, it would be easy as pie. Technical 13, I'm from pt.wiki, there is no such thing, thank you for the support. I'll try again in the future... Cainamarques (talk) 17:52, 3 December 2013 (UTC)[reply]

Another

{{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;"><!-- 1em/8 : equivalent to a "fine space" -->!</span> |pattern= %b<> }}}} → 45
{{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;">!</span> |pattern= %b<> }}}} → 45
{{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left:.125em;">!</span> |pattern= %b<> }}}} → 43

The same when using pattern=<.->:

{{#invoke:String|len|s={{#invoke:String|replace|source= <span style="padding-left: 0.125em;"><!-- 1em/8 : equivalent to a "fine space" -->!</span> |pattern= <.-> }}}} → 45
{{#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<> }}}} → 34

What to do? I am trying to get a result of "1". --Jerome Potts (talk) 19:00, 28 May 2014 (UTC)[reply]

The string module defaults to |plain=true which means the search pattern is plain text and is not a regular expression. To fix, add |plain=false in the above. Johnuniq (talk) 02:38, 29 May 2014 (UTC)[reply]
Oops ! Thank you much. --Jerome Potts (talk) 05:05, 29 May 2014 (UTC)[reply]

"Replace" behaves differently when I name parameters vs. when I don't

In Template:AfC comment/sandbox if I specify source= or pattern= 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? wbm1058 (talk) 17:14, 4 February 2021 (UTC)[reply]

@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 |source= parameter. Here's how it works in Template:AfC comment and Template:AfC comment/sandbox (I've shown {{tl|paragraph break}} so you can see where it would appear):

Positional parameters:

{{#invoke:String|replace|


|
+|{{tl|paragraph break}}||false}}

Result: {{paragraph break}}

Named parameters:

{{#invoke:String|replace|source=


|pattern=
+|replace={{tl|paragraph break}}|count=|plain=false}}

Result:

Hope that helps. --RexxS (talk) 17:52, 4 February 2021 (UTC)[reply]

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:

{{#ifexpr: {{#invoke:string|find|{{:{{SUBJECTPAGENAME}}}}|<search string>}}|<string found>|<string not found>}}}}

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:

{{#ifexpr: {{#invoke:string|find|{{#invoke:string|sub|{{:{{SUBJECTPAGENAME}}}}|1|500}}|<search string>}}|<string found>|<string not found>}}}}

This works if SUBJECTPAGENAME is replaced with the actual subject page name but fails with this error when I try substituting the {{#invoke:string|sub:

String Module Error: String subset index out of range

I assume the index (500) is out of range because it's trying to search the raw code {{:{{SUBJECTPAGENAME}}}} rather than the transcluded page because the page is >500 characters long and the template loop call is located after the 500th character.

{{#invoke:string|sub|{{:{{SUBJECTPAGENAME}}}}|1|500}}

works fine when it stands alone. The index out-of-range error only happens when this is nested inside of the {{#invoke:string|find so I assume this syntax is just too complex.

Is there an {{#invoke:string|find-in-sub| 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. – wbm1058 (talk) 15:02, 18 February 2022 (UTC)[reply]

|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 – wbm1058 (talk) 15:17, 18 February 2022 (UTC)[reply]
(edit conflict) This is an XY problem. Try using Module:Page to get the raw wikitext of the page ({{#invoke:page|getContent|{{SUBJECTPAGENAME}}}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 {{SUBPAGENAME}} as well, or otherwise it doesn't get expanded when you try to substitute the string call. * Pppery * it has begun... 15:18, 18 February 2022 (UTC)[reply]
Thanks 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 Talk:David Ayers. That is populating Category:Articles with talk page redirects. But if I {{subst: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. – wbm1058 (talk) 16:06, 18 February 2022 (UTC)[reply]
Does Special:Diff/1072625351 fix the problem? * Pppery * it has begun... 16:57, 18 February 2022 (UTC)[reply]
Indeed that does! Doh. I initially did try searching for #invoke:RfD but wasn't finding it because of the transclusion! And {{#invoke:page|getContent|{{SUBJECTPAGENAME}}|as=raw}} 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! wbm1058 (talk) 17:55, 18 February 2022 (UTC)[reply]

Extract number

I am trying to extract a number from a string and ignore any units. I have been using {{first word}} which works in some cases.

  • {{first word|1={{convert|45|m}}}} -> 45
  • {{first word|1={{convert|18.6|nmi|km mi|abbr=on}}}} -> 18.6 nmi

In the second case it doesn't work. I think because a nbsp is being used instead of a space. Is there any template which will work for both? — Martin (MSGJ · talk) 08:20, 11 April 2022 (UTC)[reply]

You can use Special:ExpandTemplates to see that convert outputs 18.6&nbsp;nmi (34.4&nbsp;km; 21.4&nbsp;mi) for the second case above. What is the purpose of this? If it's for convert and if you can control the parameters, using abbr=values would make selection easier. For the second case above, that would give 18.6 (34.4; 21.4). Johnuniq (talk) 11:01, 11 April 2022 (UTC)[reply]
It's for importing data into Wikidata from infobox fields. I can't control the parameters because convert is used separately on each article. — Martin (MSGJ · talk) 19:36, 11 April 2022 (UTC)[reply]

Template:Str rep pattern match irregularities

Editors interested in this module may be interested in the following discussion: Template talk:Str rep#Pattern match irregularities. Your feedback would be welcome. Mathglot (talk) 08:14, 8 October 2022 (UTC)[reply]

How to do string.replace of level3 headers in transcluded wikitext

I am trying to use replace to strip out the level3 headers from a transclusion of Dublin North-West (Dáil constituency)#TDs.

However {{#invoke:String|replace|{{trim|{{#section-h:Dublin North-West (Dáil constituency)|TDs}}}}|%=%=%=[^%=]*%=%=%=||plain=false}} leaves the level3 headers in place -- see my /sandbox test page at special:permalink/1164910747

Any suggestions? Feel free to edit that sandbox if it helps. BrownHairedGirl (talk) • (contribs) 21:13, 11 July 2023 (UTC)[reply]

I have never seen the #section-h construction before. Would you mind explaining where that comes from? — Martin (MSGJ · talk) 21:50, 11 July 2023 (UTC)[reply]
See Help:Labeled section transclusion. * Pppery * it has begun... 22:09, 11 July 2023 (UTC)[reply]
Turns out this has nothing to do with Lua: when using an equals sign in an argument to a template or module you need to name the parameter explicitly or escape it with {{=}}: {{#invoke:String|replace|{{trim|{{#section-h:Dublin North-West (Dáil constituency)|TDs}}}}|pattern=%=%=%=[^%=]*%=%=%=|replace=|plain=false}} should work. * Pppery * it has begun... 22:09, 11 July 2023 (UTC)[reply]
Bless you @Pppery. That's done it. Thanks also to @Trappist the monk for appplyig the fix to my sandbox -- I am not sure which of you did what first, but you have both been a great help, and mighty promptly.
I case you are curious, this is for my development of the {{Constituency Teachtaí Dála navbox}} meta-template. When I used it to make {{Kerry (Dáil constituency)/TDs}}, the level3 headers were an annoying intrusion, but now I can make 'em vanish from the navbox. Thanks again. BrownHairedGirl (talk) • (contribs) 22:35, 11 July 2023 (UTC)[reply]
This isn't quite what you were asking, but related enough you might be interested in it: {{Conditional heading}}. Mathglot (talk) 04:50, 12 July 2023 (UTC)[reply]
Thanks, @Mathglot. I want to remove the headings rather than to promote or demote them, so it's not what I need in this case. But it's good to know about this, for future refrence. BrownHairedGirl (talk) • (contribs) 23:11, 12 July 2023 (UTC)[reply]