Jump to content

Module:Str find word/doc: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Line 8: Line 8:


For example, in the source string '{{mono|1=foo, bar}}' the word '{{mono|1=bar}}' appears, but the word '{{mono|1=november}}' does not.
For example, in the source string '{{mono|1=foo, bar}}' the word '{{mono|1=bar}}' appears, but the word '{{mono|1=november}}' does not.
:<code><nowiki>{{#invoke:Str find word|main |source=foo, bar |word=bar}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; {{#invoke:Str find word|main |source=foo, bar |word=bar}}
:<code><nowiki>{{#invoke:Str find word |main |source=foo, bar |word=bar}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; {{#invoke:Str find word |main |source=foo, bar |word=bar}}
:<code><nowiki>{{#invoke:Str find word|main |source=alpha, beta, gamma |word=november}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; {{#invoke:Str find word|main |source=alpha, beta, gamma |word=november}}
:<code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |word=november}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |word=november}}


''Multiple word check'': The search can be extended to check for multiple words being present.
''Multiple word check'': The search can be extended to check for multiple words being present.


''AND''-list of words, all words must be present:
''AND''-list of words, all words must be present:
:<code><nowiki>{{#invoke:Str find word|main |source=alpha, beta, gamma |andwords=beta, alpha}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, alpha}}
:<code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, alpha}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, alpha}}
:<code><nowiki>{{#invoke:Str find word|main |source=alpha, beta, gamma |andwords=beta, omega}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, omega}}
:<code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, omega}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, omega}}


''OR''-list of words, at least one off the words must be present:
''OR''-list of words, at least one off the words must be present:
:<code><nowiki>{{#invoke:Str find word|main |source=alpha, beta, gamma |orwords=beta, alpha}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, alpha}}
:<code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, alpha}}</nowiki></code> <small>({{code|1=True}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, alpha}}
:<code><nowiki>{{#invoke:Str find word|main |source=alpha, beta, gamma |orwords=beta, omega}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, omega}}
:<code><nowiki>{{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, omega}}</nowiki></code> <small>({{code|1=False}})</small> &rarr; {{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, omega}}


Complete words: the complete word is '{{mono|1=foobar}}' (example needed)
Complete words: the complete word is '{{mono|1=foobar}}' (example needed)
Line 61: Line 61:
| {{para|1=source|2=alpha, foo, lima}}
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foo}}
| {{para|word|foo}}
| {{#invoke:Str find word|main |explain=true |source=alpha, foo, lima |word=foo}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foo}}
| {{aye}}
| {{aye}}
|
|
Line 67: Line 67:
| {{para|1=source|2=alpha, foo, lima}}
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|bar}}
| {{para|word|bar}}
| {{#invoke:Str find word|main |explain=true |source=alpha, foo, lima |word=foo-bar}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foo-bar}}
| {{nay}}
| {{nay}}
| full word ''bar'' not in the source
| full word ''bar'' not in the source
Line 73: Line 73:
| {{para|1=source|2=alpha, foo, lima}}
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foobar}}
| {{para|word|foobar}}
| {{#invoke:Str find word|main |explain=true |source=alpha, foo, lima}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima}}
| {{nay}}
| {{nay}}
| word ''foobar'' not in the source as full word
| word ''foobar'' not in the source as full word
Line 93: Line 93:
| {{para |andw|delta, lima}}
| {{para |andw|delta, lima}}
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta, lima}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta, lima}}
| both words present in the source
| both words present in the source
|-
|-
Line 99: Line 99:
| {{para |andw|foo, nov}}
| {{para |andw|foo, nov}}
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta}}
| AND-logic: A word in the ''AND''-list is not present ({{mono|nov}}), so false
| AND-logic: A word in the ''AND''-list is not present ({{mono|nov}}), so false
|-
|-
Line 105: Line 105:
| {{para |andw|delta}}
| {{para |andw|delta}}
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
| {{para|w}} and {{para |andw}} are combined
| {{para|w}} and {{para |andw}} are combined
|-
|-
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para|1=s|2=alpha, foo, lima, delta}}
| {{para |andw|bar, april}}
| {{para |andw|bar, april}}
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
|}
|}


Line 128: Line 128:
|
|
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta, lima}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta, lima}}
| both words present in the source
| both words present in the source
|-
|-
Line 134: Line 134:
|
|
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta}}
|
|
|-
|-
Line 140: Line 140:
| {{para |andw|delta}}
| {{para |andw|delta}}
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
| {{para|w}} and {{para |andw}} are combined
| {{para|w}} and {{para |andw}} are combined
|-
|-
Line 146: Line 146:
|
|
| {{para |orw|delta, lima}}
| {{para |orw|delta, lima}}
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |orw=delta, lima}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |orw=delta, lima}}
| both OR-words are present in the source
| both OR-words are present in the source
|-
|-
Line 152: Line 152:
|
|
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |orw=foo, nov}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |orw=foo, nov}}
| OR-logic: one of the words is present ({{para|foo}}), so true
| OR-logic: one of the words is present ({{para|foo}}), so true
|-
|-
Line 158: Line 158:
| {{para |andw|alpha, lima}}
| {{para |andw|alpha, lima}}
| {{para |orw|foo, nov}}
| {{para |orw|foo, nov}}
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=alpha, lima |orw=foo, nov}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=alpha, lima |orw=foo, nov}}
|
|
|-
|-
Line 164: Line 164:
| {{para |andw|bar, april}}
| {{para |andw|bar, april}}
| {{para |orw|nov, dec}}
| {{para |orw|nov, dec}}
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta |orw=nov, dec}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta |orw=nov, dec}}
| none of the OR-words is present, so false
| none of the OR-words is present, so false
|}
|}
Line 184: Line 184:
| {{para |andw|delta, lima}}
| {{para |andw|delta, lima}}
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta, lima}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta, lima}}
| both words present in the source
| both words present in the source
|-
|-
Line 190: Line 190:
| {{para |andw|foo, nov}}
| {{para |andw|foo, nov}}
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta}}
| AND-logic: A word in the ''AND''-list is not present ({{mono|nov}}), so false
| AND-logic: A word in the ''AND''-list is not present ({{mono|nov}}), so false
|-
|-
Line 196: Line 196:
| {{para |andw|delta}}
| {{para |andw|delta}}
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta}}
| {{para|w}} and {{para |andw}} are combined
| {{para|w}} and {{para |andw}} are combined
|-
|-
Line 202: Line 202:
|
|
| {{para |orw|delta, lima}}
| {{para |orw|delta, lima}}
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |orw=delta, lima}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |orw=delta, lima}}
| both OR-words are present in the source
| both OR-words are present in the source
|-
|-
Line 208: Line 208:
|
|
|
|
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |orw=foo, nov}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |orw=foo, nov}}
| OR-logic: one of the words is present ({{para|foo}}), so true
| OR-logic: one of the words is present ({{para|foo}}), so true
|-
|-
Line 214: Line 214:
| {{para |andw|alpha, lima}}
| {{para |andw|alpha, lima}}
| {{para |orw|foo, nov}}
| {{para |orw|foo, nov}}
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=alpha, lima |orw=foo, nov}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=alpha, lima |orw=foo, nov}}
|
|
|-
|-
Line 220: Line 220:
| {{para |andw|bar, april}}
| {{para |andw|bar, april}}
| {{para |orw|nov, dec}}
| {{para |orw|nov, dec}}
| {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima, delta |andw=delta |orw=nov, dec}}
| {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima, delta |andw=delta |orw=nov, dec}}
| none of the OR-words is present, so false
| none of the OR-words is present, so false
|}
|}
Line 259: Line 259:
| {{para|w|foo}}
| {{para|w|foo}}
|
|
| &rarr; {{#invoke:Str find word|main |explain=true |s=alpha, foo, lima|w=foo|out-true=yes|out-false=no}}
| &rarr; {{#invoke:Str find word |main |explain=true |s=alpha, foo, lima|w=foo|out-true=yes|out-false=no}}
|
|
|-
|-
Line 265: Line 265:
| {{para|w|foobar}}
| {{para|w|foobar}}
|
|
| &rarr; {{#invoke:Str find word|main |explain=true |source=alpha, foo, lima |word=foobar|out-true=yes|out-false=no}}
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foobar|out-true=yes|out-false=no}}
|
|
|-
|-
Line 271: Line 271:
| {{para|w|foo}}
| {{para|w|foo}}
|
|
| &rarr; {{#invoke:Str find word|main |explain=true |source=alpha, foobar, lima |word=foo|out-true=yes|out-false=no}}
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, foobar, lima |word=foo|out-true=yes|out-false=no}}
| 'foo' is not the complete word
| 'foo' is not the complete word
|-
|-
Line 277: Line 277:
| {{para|w|foo}}
| {{para|w|foo}}
|
|
| &rarr; {{#invoke:Str find word|main |explain=true |source=alpha, FOO, lima |word=foo|out-true=yes|out-false=no|case=false}}
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, FOO, lima |word=foo|out-true=yes|out-false=no|case=false}}
| {{para|case|false}}: a=A ''(default)''
| {{para|case|false}}: a=A ''(default)''
|-
|-
Line 283: Line 283:
| {{para|w|foo}}
| {{para|w|foo}}
|
|
| &rarr; {{#invoke:Str find word|main |explain=true |source=alpha, FOO, lima |word=foo|out-true=yes|out-false=no|case=true}}
| &rarr; {{#invoke:Str find word |main |explain=true |source=alpha, FOO, lima |word=foo|out-true=yes|out-false=no|case=true}}
| {{para|case|true}}: a=/=A
| {{para|case|true}}: a=/=A
|}
|}
Line 294: Line 294:
| {{para|1=source|2=alpha, foo, lima}}
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foo}}
| {{para|word|foo}}
| {{#invoke:Str find word|main |explain=true |source=alpha, foo, lima |word=foo|out-true=Yes, a hit|out-false=No, not found}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foo|out-true=Yes, a hit|out-false=No, not found}}
| {{aye}}
| {{aye}}
|-
|-
| {{para|1=source|2=alpha, foo, lima}}
| {{para|1=source|2=alpha, foo, lima}}
| {{para|word|foobar}}
| {{para|word|foobar}}
| {{#invoke:Str find word|main |explain=true |source=alpha, foo, lima |word=foobar|out-true=Yes, a hit|out-false=No, not found}}
| {{#invoke:Str find word |main |explain=true |source=alpha, foo, lima |word=foobar|out-true=Yes, a hit|out-false=No, not found}}
| {{nay}}
| {{nay}}
|}
|}

Revision as of 15:15, 25 March 2023

Module:Str find word/links/doc Implements template {{Str find word}}.

This module looks for a word being present in a comma-separated list of words. It then returns a True or False value. By default, the True-value returned is the found word itself; the False-value is a blank string.

For example, in the source string 'foo, bar' the word 'bar' appears, but the word 'november' does not.

{{#invoke:Str find word |main |source=foo, bar |word=bar}} (True) → bar
{{#invoke:Str find word |main |source=alpha, beta, gamma |word=november}} (False)

Multiple word check: The search can be extended to check for multiple words being present.

AND-list of words, all words must be present:

{{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, alpha}} (True) → beta,alpha
{{#invoke:Str find word |main |source=alpha, beta, gamma |andwords=beta, omega}} (False)

OR-list of words, at least one off the words must be present:

{{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, alpha}} (True) → beta,alpha
{{#invoke:Str find word |main |source=alpha, beta, gamma |orwords=beta, omega}} (False) → beta

Complete words: the complete word is 'foobar' (example needed) Whitespace is trimmed,

The module is aimed at template code usage. (The editor does not have to apply Lua patterns like [%a%d]*). }}

Parameters

Main parameters (including their synonyms)
{{Str find word
|source= s=
 |word= andwords= andw=
 |orwords= orw=
|case=
|out-true=
|out-false=
}}
{{Str find word |s= |w= |andw= |orw= |case= |out-true= |out-false=}}

Basic: word is in source-wordlist?

{{Str find word
|source =
|word   =
}}
{{Str find word |source=cow, horse, dog, fish |word=dog}} (True) → dog
{{Str find word |source=cow, horse, dog, fish |word=cat}} (False)


True/False return value

When the word is found in the source (True), the function returns the word itself (by default), or |out-true= value (when entered non-empty).
When not found (False), the function returns a empty string (''), or the |out-false= value when entered.
|source=alpha, foo, lima |word=foo foo Green tickY
|source=alpha, foo, lima |word=bar Red XN full word bar not in the source
|source=alpha, foo, lima |word=foobar Red XN word foobar not in the source as full word

AND-wordlist checks

|word= can be a list of words. All will be checked with the AND-requirement (each word must be present).
|andwords= accepts a list of words that all will be checked with the AND-requirement (each word must be present).
Alway, both |word= and |andwords= are combined into one single AND-wordlist.
|source=
|s=
|andwords=
|word=
result note
|s=alpha, foo, lima, delta |andw=delta, lima delta,lima both words present in the source
|s=alpha, foo, lima, delta |andw=foo, nov AND-logic: A word in the AND-list is not present (nov), so false
|s=alpha, foo, lima, delta |andw=delta delta |w= and |andw= are combined
|s=alpha, foo, lima, delta |andw=bar, april delta

OR-wordlist checks

|orwords=, |orw=
Accepts a list of words that all will be checked by the OR-requirement ("at least one of the words must be present).
|source=
|s=
|orwords= result note
|s=alpha, foo, lima, delta delta,lima both words present in the source
|s=alpha, foo, lima, delta
|s=alpha, foo, lima, delta |andw=delta delta |w= and |andw= are combined
|s=alpha, foo, lima, delta |orw=delta, lima delta,lima both OR-words are present in the source
|s=alpha, foo, lima, delta foo OR-logic: one of the words is present (|foo=), so true
|s=alpha, foo, lima, delta |andw=alpha, lima |orw=foo, nov alpha,lima,foo
|s=alpha, foo, lima, delta |andw=bar, april |orw=nov, dec none of the OR-words is present, so false

both AND-words and OR-words to check

|andwords= |orwords=
checks both lists for their own check: "ANd-words" must all be present, "OR-words" must have at least one present.
Result: True if both check results are True, otherwise False.
|source=
|s=
|andwords=
|word=
|orwords= result note
|s=alpha, foo, lima, delta |andw=delta, lima delta,lima both words present in the source
|s=alpha, foo, lima, delta |andw=foo, nov AND-logic: A word in the AND-list is not present (nov), so false
|s=alpha, foo, lima, delta |andw=delta delta |w= and |andw= are combined
|s=alpha, foo, lima, delta |orw=delta, lima delta,lima both OR-words are present in the source
|s=alpha, foo, lima, delta foo OR-logic: one of the words is present (|foo=), so true
|s=alpha, foo, lima, delta |andw=alpha, lima |orw=foo, nov alpha,lima,foo
|s=alpha, foo, lima, delta |andw=bar, april |orw=nov, dec none of the OR-words is present, so false

Edge cases

Blank list(s)
Empty input string(s) will conclude negative:
|source, andwords, orwords=<blank>
|source=<blank> |word=beta Red XN
|source=alpha, beta, gamma |word=<blank> Red XN

untested; noinclude from main /doc

As of Mar 2023, not supported.

casesensitive

|casesensitive=true, |case=true Case-sensistive (A=/=a) DEFAULT
|casesensitive=false, |case=false Not case-sensistive (A=a)
|source=
|s=
|word=
|w=
|case= result note
|s=alpha, foo, lima |w=foo → foo
|s=alpha, foo, lima |w=foobar
|s=alpha, foobar, lima |w=foo 'foo' is not the complete word
|s=alpha, FOO, lima |w=foo → foo |case=false: a=A (default)
|s=alpha, FOO, lima |w=foo |case=true: a=/=A

Rephrase True, False result: |out-true=, out-false=

|out-true=out-true, a hit
|out-false=out-falset found
|source=alpha, foo, lima |word=foo foo Green tickY
|source=alpha, foo, lima |word=foobar Red XN

boolean words

|booleans=
|booleans=true will convert all boolean words into "True/False".
per module:Yesno (Yes, true, yes, 1 = true) (todo: document value 2?, nil, blanketc. Irrespective of case?)
other words not changed ("horse" remains horse, not T/F).
Note: per logic definition, the word "false" is returned (as being found).
|booleans=true
|source=YeS |word=true true
|source=0 |word=FalSe false note: the word "false" is found and returned

quotes, escape

under construction

synonyms

under construction


Errors and analysis

  • The logic itself does not produce errors. There is always a True of False result.
There are no logical errors foreseen: any flaw in the input will return a False result (for example, no input strings to check were entered).
  • |explain=true: when in Preview, display a report on input, output and the logical steps (debug).
  • |explain=testcases: When set in ns Template or User on subpage /testcases, the Preview report is static (shown permanently).