Difference: SearchPatternCookbook (3 vs. 4)

Revision 42007-01-14 - TWikiContributor

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="FormattedSearch"
 

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.

Changed:
<
<
>
>
 
Added:
>
>
<-- ========================== -->
 

Pattern 1: Extract values from a table

Problem definition

Line: 27 to 32
 
<form>
<select>
Changed:
<
<
>
>
 
Line: 35 to 40
 which is, in effect:
Changed:
<
<
>
>
 
Deleted:
<
<

 
Added:
>
>
<-- ========================== -->
 

Pattern 2: List generated from form classification

Problem

Line: 61 to 70
 
%SEARCH{"[T]opicClassification.*value\=.*Two;[T]opicStatus.*value\=.*%URLPARAM{type}%"
Changed:
<
<
regex="on" casesensitive="on" nosearch="on"
>
>
type="regex" casesensitive="on" nosearch="on"
 format=" * $topic - last modified by $wikiusername on $date
    $formfield(TopicStatus) "
Line: 75 to 84
 Filter:
Changed:
<
<
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="TopicClassification" web="TWiki" regex="on"
>
>
%SEARCH{ "^\|[^\|]*\| *option *\|" topic="TopicClassification" web="TWiki" type="regex"
 multiple="on" nosearch="on" nototal="on" format="" }%
Line: 83 to 92
 

This will create similar functionality as TWiki:Plugins.TopicClassificationAddOn

Deleted:
<
<

 
Added:
>
>
<-- ========================== -->
 

Pattern 3: Creating lists of TWiki usernames

Problem

Line: 139 to 152
 
Changed:
<
<
Number of topics: 144
<--/patternSearchResultCount-->

>
>
Number of topics: 144
<--/patternSearchResultCount-->

 
Line: 149 to 162
 
  • META:FORM.*[U]serForm will search for all topics with a UserForm attached - change this if you have a different form where userdata is stored. Please note that this search does not actually extract anything from the form - it just uses it to identify the appropriate pages
  • excludetopic="Test*, TWiki*" allows to skip all topics starting with Test and TWiki, such as TestUser or TWikiAdmin. Use this if you have any special users who you do not want appearing in this list
Added:
>
>
<-- ========================== -->
 

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?

Changed:
<
<

Solution

>
>

Solution 1: Using META

Since TWiki 4.0 you can now use the META variable:

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

Solution 2: Using SpreadSheetPlugin

  You might think that the following Search would do the trick:
Changed:
<
<
parent_link
>
>
 
Changed:
<
<
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]]
>
>
However, the $parent link fails if the topic has no parent set ($parent will be empty). You can use some TWiki:Plugins/SpreadSheetPlugin magic to conditionally link to the parent or to WebHome:

$percntCALC{$IF($EXACT($parent,),<nop>,$NOP(   * $parent))}$percnt
  So the total Search query to find a topic's parent topic is:
Changed:
<
<
[[FormattedSearch][parent_link]]
>
>

Test Case

The parent topic of this topic is:

Solution 3: Using IF statement

This pattern can be rewritten using %IF%, removing the dependency on SpreadSheetPlugin:
%SEARCH{ "^%BASETOPIC%$" web="%BASEWEB%" scope="topic" type="regex" nonoise="on" format="$percntIF{$quot$parent$quot then=$quot   * $parent$quot else=$quot<nop>$quot}$percnt" }%
 
Changed:
<
<

Test Case

The parent topic of this topic is: FormattedSearch
>
>

Test Case

The parent topic of this topic is:
 
Changed:
<
<

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

>
>
<-- ========================== -->

Pattern 5: Show all Children of a given topic

Problem

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

Solution

The parent information is stored in the META:TOPICPARENT meta data. Do a SEARCH to find all topic parent meta data pointing to the current topic:

Children:
%SEARCH{ "META\:TOPICPARENT.*\"%TOPIC%\"" type="regex" nonoise="on" format="[[$topic]]" separator=", " }%

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

<-- ========================== -->

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

 

Problem

Line: 218 to 280
 
Changed:
<
<

Pattern 6: Extract a value from a bullet list

>
>
<-- ========================== -->

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

Problem

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

Solution

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>"}%
</select>

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 8: Extract a value from a named bullet list item

 

Problem

Line: 229 to 325
 Search for the Name: entry.
Changed:
<
<
>
>
 

Test case

Line: 240 to 336
  Search result:
Changed:
<
<
>
>

<-- ========================== -->

Pattern 9: Search for Form and Meta data: explained

 
Deleted:
<
<

Pattern 7: Search for Form and Meta data: explained

 

Problem

Added:
>
>
 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?
Line: 249 to 350
 
  • what is the meaning of the td..td in the search expression?


Changed:
<
<
%SEARCH{ "[S]tatus.*(td..td|value\=).*[W]aiting" casesensitive="on" regex="on"
>
>
%SEARCH{ "[S]tatus.*(td..td|value\=).*[W]aiting" casesensitive="on" type="regex"
 nosearch="on" nototal="on" format="| $topic<br /> ($date - $rev - Diffs) |"}%
Line: 263 to 365
  So a search for a form field could look like:

Changed:
<
<
%SEARCH{ "[O]peratingSystem.*value\=.*[O]sWin" regex="on" ... }%
>
>
%SEARCH{ "[O]peratingSystem.*value\=.*[O]sWin" type="regex" ... }%
 
  • 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
Line: 274 to 376
  The following search finds topics in the old and new format:

Changed:
<
<
%SEARCH{ "[O]peratingSystem.*(td..td|value\=).*[O]sWin" regex="on" ... }%
>
>
%SEARCH{ "[O]peratingSystem.*(td..td|value\=).*[O]sWin" type="regex" ... }%
 

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.

Changed:
<
<

Pattern 8: Search all topics that have been moved

>
>
<-- ========================== -->

Pattern 10: Search all topics that have been moved

 

Problem

Added:
>
>
 How would I go about listing all moved topics ?

Solution

Line: 286 to 393
 How would I go about listing all moved topics ?

Solution

Added:
>
>
 Search for the META:TOPICMOVED meta data. Type this:
Changed:
<
<
Moved topics: %SEARCH{ "%META\:TOPICMOVED" regex="on" format="$topic, " nosearch="on" noheader="on" nosummary="on" }%
>
>
Moved topics: %SEARCH{ "%META\:TOPICMOVED" type="regex" format="$topic, " nosearch="on" noheader="on" nosummary="on" }%
  to get this (limited to 10 results):
Changed:
<
<
Moved topics:
Number of topics: 0
<--/patternSearchResultCount-->

Contributors

>
>
Moved topics:
Number of topics: 0
<--/patternSearchResultCount-->

 
Changed:
<
<
TWiki:Main.AntonAylward, TWiki:Main.ArthurClemens, TWiki:Main.JosMaccabiani, TWiki:Main.PeterThoeny, TWiki:Main.SueLocke
>
>
Related Topics: UserDocumentationCategory, SearchHelp, TWikiVariables#VarSEARCH, FormattedSearch, RegularExpression
 
Changed:
<
<
Related Topics: UserDocumentationCategory
>
>
-- Contributors: TWiki:Main.AntonAylward, TWiki:Main.ArthurClemens, TWiki:Main.JosMaccabiani, TWiki:Main.PeterThoeny, TWiki:Main.SueLocke
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback
Note: Please contribute updates to this topic on TWiki.org at TWiki:TWiki.SearchPatternCookbook.