# Search Pattern Cookbook The Search function in TWiki is very powerful. Especially searches using a [[RegularExpression]] play an important part of tapping TWiki's full potential. Unfortunately [[RegularExpressions]] can be incredibly obscure to the uninitiated. Most people not familiar (enough) with Regular Expressions mostly cut and paste (and maybe tweak) from existing examples. This page intends to collect lots of examples together.
## Pattern 1: Extract values from a table ### Problem definition Suppose there is a topic with a table defining entries in a TWikiForm. I.e. they define select menu items in a form template. They are then formatted like: | *Name* | *Type* | *Tooltip message* | | option1 | option | | | option2 | option | | | option3 | option | | How to extract the 'name' values, i.e. 'option1', 'option2' and 'option3' and put them in a HTML form select input? ### Solution The following search pattern can be employed:
which is, in effect:
%BR% ## Pattern 2: List generated from form classification ### Problem Imagine a TWiki form-based topic classification, i.e. every page has a form with several fields. How to: 1. create a search to display all topics where one form field is set to a certain value 2. create a search to filter the list above based on the values of a second form field ### Test case In practice: %BR% Image a TWiki form with two fields: - TopicClassification = One, Two or Three - TopicStatus = Test or Final We will: 1. List all topics where the TopicClassification field is set to 'Two' 2. Enable the user to filter this list based on the values of TopicStatus ### Solution %SEARCH{"[T]opicClassification.*value\=.*Two;[T]opicStatus.*value\=.*%URLPARAM{type}%" regex="on" casesensitive="on" nosearch="on" format=" * $topic - _last modified by_ $wikiusername _on_ $date %BR%     $formfield(TopicStatus) " sort="topic"}% The filtering select dialogue is created as in Pattern 1: %STARTSIDEBAR% *Filter:* %BR%
%STOPSIDEBAR% This will create similar functionality as TWiki:Plugins.TopicClassificationAddOn %BR% ## Pattern 3a: listbox with all user names ### Problem How to populate a list box with all usernames of registered TWiki users ### Solution
Which expands to this: (here limited to all Z\* users because TWiki.org has so many)
This searches all topics in the Main web that contain "Name", "Email" and "Country" bullets. Alternatively, do a [[FormattedSearch]] with `multiple="on"` on the [[Main.TWikiUsers|Main/TWikiUsers]] topic. ## Pattern 3b: listbox with all user names - select multiple names ### Problem Suppose you want to send mail from a form on topic page to a selected list of **_multiple_** [[TWikiUsers]] ### Solution The example of Pattern 3a produces the list box. Add a MULTIPLE to the _select_ statement, i.e.:

TWiki Installation Error

Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags) ## Pattern 4: Extract the parent of a given topic ### Problem How to get to the parent of the current topic to display on the page? ### Solution You might think that the following Search would do the trick: %SEARCH{ "^%BASETOPIC%$" scope="topic" nosearch="on" type="regex" nototal="on" format="[[$parent][parent_link]]" }% However, the `[[$parent][parent_link]]` link fails if the topic has no parent set (`$parent` will be empty). You can use some [[SpreadSheetPlugin]] magic to conditionally link to the parent or to `WebHome`: `[[$percntCALC{$IF($EXACT($parent,), %HOMETOPIC%, $parent)}$percnt][parent_link]]` So the total Search query to find a topic's parent topic is: %SEARCH{ "^%BASETOPIC%$" scope="topic" nosearch="on" type="regex" nototal="on" format="[[$percntCALC{$IF($EXACT($parent,), %HOMETOPIC%, $parent)}$percnt][parent_link]]" }% ### Test Case The parent topic of this topic is: # TWiki Installation Error Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags) ## Pattern 5: Search and display the home topics of public webs in a list ### Problem How to find and display public webs in a drop down list box. ### Solution _Thanks to TWiki:Main.PeterThoeny for these solutions._
### Test case Public webs of TWiki.
%T% For private webs, or any other webs you wish to exclude from the display, use "on" for the `Exclude web from a web="all" search` setting in the relevant web's WebPreferences topic. ### Alternative solution This result can also be accomplished with the %WEBLIST% variable.
## Pattern 6: Extract a value from a bullet list ### Problem Display the user name in the user's topic title ### Solution Search for the `Name:` entry. %SEARCH{" * [N]ame: " topic="%TOPIC%" regex="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}% ### Test case To create a test case, we will put a name entry here: - Name: John Doe Search result: # TWiki Installation Error Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags) ## Pattern 7: Search for Form and Meta data: explained ### Problem Below is an example of a search that searches form data. The questions are: - why is this searching the metadata, shouldn't it just search the text? - what is the meaning of the `td..td` in the search expression? %SEARCH{ "[S]tatus.*(td..td|value\=).*[W]aiting" casesensitive="on" regex="on" nosearch="on" nototal="on" format="| [[$topic]]
($date - $rev - [[%SCRIPTURLPATH{rdiff}%/$web/$topic][Diffs]]) |"}% ### Solution %SEARCH depends on grep, and grep searches the whole file, including the meta data. An example meta data form field is: %META:FIELD{name="OperatingSystem" title="OperatingSystem" value="OsWin"}% So a search for a form field could look like: %SEARCH{ "[O]peratingSystem.*value\=.*[O]sWin" regex="on" ... }% - Using square brackets is a trick to avoid a hit on the topic doing the search. - The `.*` indicate that there can be any number of any character between `OperatingSystem` and `value` in the (whole) file Now the original file format of the category table (the predecessor of the TWiki forms) looks like this: OperatingSystem: OsWin The following search finds topics in the old and new format: %SEARCH{ "[O]peratingSystem.*(td..td|value\=).*[O]sWin" regex="on" ... }% The `td..td` matches `td<>td`; a simple search on `"[O]peratingSystem.*[O]sWin"` could find a hit in the topic text by coincidence. A simple `%SEARCH{ "[O]peratingSystem.*value\=.*[O]sWin" ...}%` search is sufficient if you do not have topics in the old format. ## Pattern 8: Search all topics that have been moved ### Problem How would I go about listing all moved topics ? ### Solution Search for the META:TOPICMOVED meta data. Type this: `Moved topics: %SEARCH{ "%META\:TOPICMOVED" regex="on" format="$topic, " nosearch="on" noheader="on" nosummary="on" }%` to get this (limited to 10 results): Moved topics: # TWiki Installation Error Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags) ## Contributors TWiki:Main.AntonAylward, TWiki:Main.ArthurClemens, TWiki:Main.JosMaccabiani, TWiki:Main.PeterThoeny, TWiki:Main.SueLocke **_Related Topics:_** [[UserDocumentationCategory]]