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:26, 30 August 2023 (Archiving 3 discussions). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 nmi (34.4 km; 21.4 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]