![]() |
![]() |
![]() |
|||||||||
|
Search Pattern CookbookThe 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.
On this page:
Pattern 1: Extract values from a table
Problem definitionSuppose 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?
SolutionThe following search pattern can be employed:
<form> <select> %SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" regex="on" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }% </select> </form> which is, in effect:
Pattern 2: List generated from form classification
ProblemImagine a TWiki form-based topic classification, i.e. every page has a form with several fields. How to:
Test caseIn practice:Image a TWiki form with two fields:
We will:
Solution
%SEARCH{"[T]opicClassification.*value\=.*Two;[T]opicStatus.*value\=.*%URLPARAM{type}%" regex="on" casesensitive="on" nosearch="on" format=" * $topic - <font face=\"arial,helvetica\" size=\"1\"> _last modified by_ $wikiusername _on_ $date </font> %BR% <font face=\"arial,helvetica\" size=\"1\"> $formfield(TopicStatus) </font>" sort="topic"}% The filtering select dialogue is created as in Pattern 1:
%STARTSIDEBAR% *Filter:* %BR% <form name="selectType" action="%SCRIPTURLPATH{"view"}%/%WEB%/" > <select name="type" size="1" onchange="document.location=this.value;"> %SEARCH{ "^\|[^\|]*\| *option *\|" topic="TopicClassification" web="%WEB%" regex="on" multiple="on" nosearch="on" nototal="on" format="<option value=%INCLUDINGTOPIC%?type=$pattern(^\| *(.*?) *\|.*)>$pattern(^\| *(.*?) *\|.*)</option>" }% <option value=%INCLUDINGTOPIC%>All pages</option> </select> </form> %STOPSIDEBAR%
This will create similar functionality as TWiki:Plugins.TopicClassificationAddOn
Pattern 3a: listbox with all user names
ProblemHow to populate a list box with all usernames of registered TWiki users
Solution
<form name="testing" action="%SCRIPTURLPATH{"view"}%/%MAINWEB%" method="get"> <select name="topic"> <option>Select user...</option> %SEARCH{ "Name:;Email:;Country:" web="%MAINWEB%" type="regex" nosearch="on" nototal="on" format="<option>$topic</option>" }% </select> <input type="submit" value="Go" /> </form> 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
Pattern 3b: listbox with all user names - select multiple names
ProblemSuppose you want to send mail from a form on topic page to a selected list of multiple TWikiUsers
SolutionThe example of Pattern 3a produces the list box. Add a MULTIPLE to the select statement, i.e.:<select name="topic" size="2" MULTIPLE> Please note that the Search pattern is unchanged compared to Pattern 3a. The change is in the HTML form element.
Test caseThe Search pattern 3a with the abovementioned modification is, in effect:
Pattern 4: Extract the parent of a given topic
ProblemHow to get to the parent of the current topic to display on the page?
SolutionYou 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 So the total Search query to find a topic's parent topic is: %SEARCH{ "^%BASETOPIC%$" scope="topic" nosearch="on" type="regex" nototal="on" format="[<nop>[$percntCALC{$IF($EXACT($parent,), <nop>%HOMETOPIC%, $parent)}$percnt][parent_link]]" }%
Test CaseThe parent topic of this topic is: FormattedSearch
Pattern 5: Search and display the home topics of public webs in a list
ProblemHow to find and display public webs in a drop down list box.
Solution
Thanks to TWiki:Main.PeterThoeny
<form> <select name="topic"> <option value="%TOPIC%">Select...</option> %SEARCH{ "%HOMETOPIC%" scope="topic" web="all" topic="%HOMETOPIC%" format="<option value=\"$web.$topic\">$web</option>" separator=" " }% </select> <input type="submit" value="Go" /> </form>
Test casePublic webs of TWiki.
Alternative solutionThis result can also be accomplished with the %WEBLIST% variable.
Pattern 6: Extract a value from a bullet list
ProblemDisplay the user name in the user's topic title
Solution
Search for the
%SEARCH{" * [N]ame: " topic="%TOPIC%" regex="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
Test caseTo create a test case, we will put a name entry here:
Search result:
Pattern 7: Search for Form and Meta data: explainedProblemBelow is an example of a search that searches form data. The questions are:
%SEARCH{ "[S]tatus.*(td..td|value\=).*[W]aiting" casesensitive="on" regex="on" nosearch="on" nototal="on" format="| [[$topic]]<br /> ($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" ... }%
Now the original file format of the category table (the predecessor of the TWiki forms) looks like this: <td valign="top" align="right"> OperatingSystem: </td><td> OsWin </td>The following search finds topics in the old and new format: %SEARCH{ "[O]peratingSystem.*(td..td|value\=).*[O]sWin" regex="on" ... }%
The
A simple
Pattern 8: Search all topics that have been movedProblemHow would I go about listing all moved topics ?
SolutionSearch for the META:TOPICMOVED meta data. Type this:
to get this (limited to 10 results): Moved topics: Number of topics: 0
Contributors
TWiki:Main.AntonAylward Related Topics: UserDocumentationCategory |