OK PolySyn verwendet Cookies. Mit Benutzung dieser Webseite stimmen Sie dem zu. Mehr Informationen dazu.

Search Pattern Cookbook

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.

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 data form. I.e. they define select menu items in a form definition. 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?


The following search pattern can be employed:

%SEARCH{ "^\|[^\|]*\| *option *\|" topic="%TOPIC%" type="regex" multiple="on" nosearch="on" nototal="on" format="<option>$pattern(^\| *(.*?) *\|.*)</option>" }%

which is, in effect:

Pattern 2: List generated from form classification


Imagine a 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:
Image a 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


%SEARCH{"TopicClassification='%URLPARAM{type}%'" type="query" nosearch="on" 
format="   * $topic - <font face=\"arial,helvetica\" size=\"1\"> 
_last modified by_ $wikiusername _on_ $date </font> %BR% &nbsp;&nbsp;&nbsp; 
<font face=\"arial,helvetica\" size=\"1\"> $formfield(TopicStatus) </font>" 

The filtering select dialogue is created as in Pattern 1:

*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%" type="regex" 
multiple="on" nosearch="on" nototal="on" format="<option value=%BASETOPIC%?type=$pattern(^\| *(.*?) *\|.*)>$pattern(^\| *(.*?) *\|.*)</option>" }% 
<option value=%BASETOPIC%>All pages</option> </select>

This will create similar functionality as Foswiki:Extensions.TopicClassificationAddOn

Pattern 3: Extract the parent of a given topic


How to get to the parent of the current topic to display on the page?

Solution 1: Using META

Use the META macro:

%META{ "parent" dontrecurse="on" }%

Pattern 4: Show all Children of a given topic


How to get to the list of all children of the current topic to display on the page?


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:

%SEARCH{ "parent.name='%TOPIC%'" type="query" nonoise="on" format="[[$topic]]" separator=", " }%

Note: Replace %TOPIC% with %BASETOPIC% if you put this SEARCH into the skin or a sidebar.

Pattern 5: Search and display the home topics of public webs in a list


How to find and display public webs in a drop down list box.


<select name="topic">
<option value="%TOPIC%">Select...</option>
%SEARCH{ "%HOMETOPIC%" scope="topic" web="all" topic="%HOMETOPIC%" format="<option value=\"$web.$topic\">$web</option>" separator=" " }%
<input type="submit"  value="Go" />

Test case

Public webs can be found with the %WEBLIST% macro.

Pattern 6: Create a select box with values from a bullet list


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.

For example, CategoryList has:
  • Clients
  • People
  • Rooms
  • Buildings


The following search pattern can be employed:

<select name="type">
<option>Select category...</option>
%SEARCH{"   *\s*.*?" topic="CategoryList" type="regex" multiple="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" format="<option>$pattern(.*   \*\s*([^\n]*).*)</option>"}%

To render the bullet list as a comma-separated list, use the separator parameter:
%SEARCH{"   *\s*.*?" topic="CategoryList" type="regex" multiple="on" casesensitive="on" nosummary="on" nosearch="on" noheader="on" nototal="on" separator="," format="$pattern(.*   \*\s*([^\n]*).*)"}%

Pattern 7: Extract a value from a named bullet list item


Display the user name in the user's topic title


Search for the Name: entry.

%SEARCH{"   * [N]ame: " topic="%TOPIC%" type="regex" 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:

Pattern 8: Search all topics that have been moved


How would I go about listing all moved topics ?


Search for the 'moved' meta data. Type this:

Moved topics: %SEARCH{ "moved.to!=''" type="query" format="$topic, " nosearch="on" noheader="on" nosummary="on" }%

Related Topics: UserDocumentationCategory, SearchHelp, Macros, FormattedSearch, RegularExpression

PolySyn | Inh.: Ingo Blickling | Stollestrasse 54 | 01159 Dresden | Sachsen | Deutschland Urheberrechte © PolySyn | Impressum | Datenschutz | powered by Foswiki