Unlike Switch parser function, this template matches any part of the input string or any Lua pattern in the input string. It returns the first match, or all matches separated by the chosen separator.

Usage edit

{{Switch by pattern|_input= |patterns1|patterns2|patterns3|keyA=patternsA |keyB=patternsB |_respectcase= |_returnall= |_returncaptures= |_sep= |_outputsep= |_default= }}

Parameters:

  • |_input= input string
  • |key=patterns return key if any of the patterns is found in the input string; patterns can be one or multiple strings separated by # (or a separator given in |_sep=); if unnamed parameters are used, the keys returned are their respective positions (1, 2, 3…)
  • |_respectcase= input string is lowercased by default, any value to avoid that
  • |_returnall= return the keys of all patterns that matched, separated by # or _outputsep; any value means yes
  • |_returncaptures= instead of keys, return captures/matches
  • |_sep= which separator is used to separate patterns (defaults to #); no pattern should contain this character or characters
  • |_outputsep= which separator to use to separate multiple keys returned when the _returnall option is on (defaults to #
  • |_default= which string to return if no patterns matched (defaults to empty string)

The string in patterns can be a Lua pattern, or a word or part of a word (always interpreted as Lua patterns!), or several such patterns separated by #, as in this#that#whatever, which will be checked as aliases until the first match is found. (this is similar to regex matching this|that|whatever, which is not available in Lua) Note that all named parameters will have leading and trailing whitespace removed. This not only includes (|string key=pattern) but also |2=pattern and similar.

Examples edit

Matching parts of the input text:

  • {{Switch by pattern|_input=[[Animalia|beasts]]|dark gray=viru|pink=animal#beast|brown=fung}} → pink
  • {{Switch by pattern|_input=[[Riboviria]]|dark gray=vir|pink=animal#beast|brown=fung}} → dark gray

With unnamed parameters, using ; to separate patterns, and returning all matches:

  • {{Switch by pattern|_input=NavyBlue and DarkRed|red|green;lime;olive|azure;blue;navy|_returnall=yes|_sep=;}} → 1#3

Alternate template name, Lua patterns (similar, but not equal to regex patterns):

  • {{reSwitch|_input=darkbrown|begins w/ brown=^brown|begins w/ dark=^dark}} → begins w/ dark
  • {{reSwitch|_input=brownish|begins w/ brown=^brown|begins w/ dark=^dark}} → begins w/ brown
  • {{reSwitch|_input=name12|has two digits=%D%d%d$|has one digit=%D%d$}} → has two digits

Return captures

  • {{Switch by pattern|''%[*(%u%a+ ?%a+)%]*''#''%[*(%u%a+ %b() ?%a+)%]*''|_respectcase=y|_returncaptures=y|_input=''[[Equus zebra]]''}}Equus zebra   (matched first pattern, two words)
  • {{Switch by pattern|''%[*(%u%a+ ?%a+)%]*''#''%[*(%u%a+ %b() ?%a+)%]*''|_respectcase=y|_returncaptures=y|_input=''[[Equus (Hippotigris) hartmannae]]''}}Equus (Hippotigris) hartmannae   (matched second pattern: Word (word) word)
  • {{Switch by pattern|''%[*(%u%a+ ?%a+)%]*''#''%[*(%u%a+ %b() ?%a+)%]*''|_respectcase=y|_returncaptures=y|_input=''[[Anser cygnoides domesticus]]''}}   (no match, three words)