Jump to content

Topic on Project:Support desk

Reading numbers from titles

4
Kiril Simeonovski (talkcontribs)

Hi. Could anybody help me how to read only numbers from titles with different parametres (e.g. the title is "1256 something" and the template should extract only "1256"; or the title is "something 358 something" and the template should extract only "358")? I tried it by using different magic words, but they appeared to be useless in this case. I'd also like to know if it's possible to create a template that will read the words from the title in a given order (e.g. the title is "Canis Minor" and the template should read only the second word, i.e. "Minor"). Thanks.

Ciencia Al Poder (talkcontribs)

For the first question, this needs something that could detect whether there's a number on a given text and extract it. It can only be done with Extension:RegexParserFunctions.

For the second one, you need to use StringFunctions: {{#explode:Canis Minor| |1}}

Technical 13 (talkcontribs)

That is not entirely true Ciencia. Kiril, would the number ever be something like "456B" and have a non-numeric number in it? If not, you can do the whole thing with just StringFunctions and the included ParserFunctions. You can use the explode function as Ciencia has said, and then you can test if the result is a number with something like {{#iferror:{{#ifexpr:{{#explode:{{{1|(string being tested)}}}| |(offset)}}^0=1}}|(do this if number)}}|(do this if not number)}} Now, the offset is the hard part, I'm assuming that you don't know where in the string the number would be, and you will likely need something like the loops extension to be able to cycle through all of the parts of the word to find it. If the number does have a non-numeric character directly attached to it, then you may need to use W:ReGex to find it. As for your second question. If the magnitude declaring word (such as minor) will always be the last word, you can pull that using {{#explode:{{{1|(string being tested)| |-1}} If it is not always the last word, then you will likely need some kind of looping functionality for that as well. I have some examples of templates where I manipulate strings in this way, and would be happy to let you look at them for reference or even help you build your template. Check out Template:Scase on my home wiki then let me know if you need help. (Note: the loops extension requires the variables extension for this functionality as well.)

Kiril Simeonovski (talkcontribs)

Thanks. This was pretty useful. I'll try it and tell you if any further assistance is needed. Best regards.

Reply to "Reading numbers from titles"