1
%META:TOPICINFO{author="ProjectContributor" date="1231502400" format="1.1" version="1"}%
2
%META:TOPICPARENT{name="FormattedSearch"}%
3
---+!! Search Pattern Cookbook
5
The Search function is very powerful. Searches using a RegularExpression play an important part of tapping Foswiki's full potential. Unfortunately RegularExpressions can be incredibly obscure to the uninitiated.
7
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.
11
<!-- ============================== -->
13
---++ Pattern 1: Extract values from a table
15
---+++ Problem definition
17
Suppose there is a topic with a table defining entries in a [[data form]]. I.e. they define select menu items in a form definition. They are then formatted like:
20
| *Name* | *Type* | *Tooltip message* |
21
| option1 | option | |
22
| option2 | option | |
23
| option3 | option | |
26
How to extract the 'name' values, i.e. 'option1', 'option2' and 'option3' and put them in a HTML form select input?
30
The following search pattern can be employed:
35
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" type="regex" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%
43
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" type="regex" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%
48
<!-- ============================== -->
49
#SearchFormClassification
50
---++ Pattern 2: List generated from form classification
54
Imagine a form-based topic classification, i.e. every page has a form with several fields. How to:
55
1. create a search to display all topics where one form field is set to a certain value
56
1. create a search to filter the list above based on the values of a second form field
61
Image a form with two fields:
62
* !TopicClassification = One, Two or Three
63
* !TopicStatus = Test or Final
66
1. List all topics where the !TopicClassification field is set to 'Two'
67
2. Enable the user to filter this list based on the values of !TopicStatus
72
%SEARCH{"TopicClassification='%URLPARAM{type}%'" type="query" nosearch="on"
73
format=" * $topic - <font face=\"arial,helvetica\" size=\"1\">
74
_last modified by_ $wikiusername _on_ $date </font> %BR%
75
<font face=\"arial,helvetica\" size=\"1\"> $formfield(TopicStatus) </font>"
79
The filtering select dialogue is created as in Pattern 1:
84
<form name="selectType" action="%SCRIPTURLPATH{"view"}%/%WEB%/" >
85
<select name="type" size="1" onchange="document.location=this.value;">
86
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="TopicClassification" web="%WEB%" type="regex"
87
multiple="on" nosearch="on" nototal="on" format="<option value=%BASETOPIC%?type=$pattern(^\| *(.*?) *\|.*)>$pattern(^\| *(.*?) *\|.*)</option>" }%
88
<option value=%BASETOPIC%>All pages</option> </select>
93
This will create similar functionality as Foswiki:Extensions.TopicClassificationAddOn
96
<!-- ============================== -->
98
---++ Pattern 3: Extract the parent of a given topic
102
How to get to the parent of the current topic to display on the page?
104
---+++ Solution 1: Using META
108
=%<nop>META{ "parent" dontrecurse="on" }%=
111
<!-- ============================== -->
113
---++ Pattern 4: Show all Children of a given topic
117
How to get to the list of all children of the current topic to display on the page?
121
The parent information is stored in the topic meta data. Do a SEARCH to find all topic parent meta data pointing to the current topic:
125
%SEARCH{ "parent.name='%TOPIC%'" type="query" nonoise="on" format="[[$topic]]" separator=", " }%
128
*Note:* Replace =%<nop>TOPIC%= with =%<nop>BASETOPIC%= if you put this SEARCH into the skin or a sidebar.
131
<!-- ============================== -->
132
#SearchPublicWebsList
133
---++ Pattern 5: Search and display the home topics of public webs in a list
137
How to find and display public webs in a drop down list box.
143
<select name="topic">
144
<option value="%TOPIC%">Select...</option>
145
%SEARCH{ "%HOMETOPIC%" scope="topic" web="all" topic="%HOMETOPIC%" format="<option value=\"$web.$topic\">$web</option>" separator=" " }%
147
<input type="submit" value="Go" />
153
Public webs can be found with the %<nop>WEBLIST% macro.
156
<select name="topic">
157
<option value="%TOPIC%">Select...</option>
158
%WEBLIST{ format="<option value=\"$name.%HOMETOPIC%\">$name</option>" webs="public" separator=" " }%
160
<input type="submit" value="Go" />
164
<!-- ============================== -->
166
---++ Pattern 6: Create a select box with values from a bullet list
170
We have a topic with a bullet list with category names. In another topic we want to offer these values in a select box dropdown.
172
For example, !CategoryList has:
180
The following search pattern can be employed:
184
<option>Select category...</option>
185
%SEARCH{" *\s*.*?" topic="CategoryList" type="regex" multiple="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="<option>$pattern(.* \*\s*([^\n]*).*)</option>"}%
189
To render the bullet list as a comma-separated list, use the =separator= parameter:
191
%SEARCH{" *\s*.*?" topic="CategoryList" type="regex" multiple="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" separator="," format="$pattern(.* \*\s*([^\n]*).*)"}%
195
<!-- ============================== -->
196
#SearchNamedBulletList
197
---++ Pattern 7: Extract a value from a named bullet list item
201
Display the user name in the user's topic title
205
Search for the =Name:= entry.
208
%SEARCH{" * [N]ame: " topic="%TOPIC%" type="regex" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
213
To create a test case, we will put a name entry here:
219
%SEARCH{" * [N]ame: " topic="%TOPIC%" type="regex" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="---+!! $pattern(.* \* Name: ([^\n]*).*)"}%
221
<!-- ============================== -->
223
---++ Pattern 8: Search all topics that have been moved
227
How would I go about listing all moved topics ?
231
Search for the 'moved' meta data. Type this:
233
=Moved topics: %<nop>SEARCH{ "moved.to!=''" type="query" format="$topic, " nosearch="on" noheader="on" nosummary="on" }%=
236
*Related Topics:* UserDocumentationCategory, SearchHelp, [[Macros]], FormattedSearch, RegularExpression