Log inRegister

Regular Expressions

Regular expressions (REs), unlike simple text queries, allow you to search for text which matches a particular pattern.

Introduction

REs are similar to (but much more powerful than) the "wildcards" used in the command-line interfaces found in operating systems such as Unix and MS-DOS. REs are supported by sophisticated search engines, as well as by many Unix-based languages and tools ( e.g., awk, grep, lex, perl, and sed ).

Examples

compan(y|ies) Search for company or companies
(peter|paul) Search for peter or paul
bug* Search for bug, bugg, buggg or simply bu (a star matches zero or more instances of the previous character)
bug.* Search for bug, bugs, bugfix (a dot-star matches zero or more instances of any character)
[Bb]ag Search for Bag, bag
b[aiueo]g Second letter is a vowel. Matches bag, bug, big
b.g Second letter is any letter. Matches also b&g
[a-zA-Z] Matches any one letter (but not a number or a symbol)
[^0-9a-zA-Z] Matches any symbol (but not a number or a letter)
[A-Z][A-Z]* Matches one or more uppercase letters
[0-9]{3}-[0-9]{2}-[0-9]{4} US social security number, e.g. 123-45-6789

%SEARCH{type="regex"}% also supports the ; and ! operators in regular expression searches.
  • ; is used to indicate an "and" search. For example, Peace;War to search for topics matching the regular expressions Peace and War.
  • ! is used to negate the sense of the following regular expression. For example, Peace;!War will find topics that match the expression Peace, but do not match the expression War.
The ";" and "!" operators are %SEARCH -specific and are not part of the standard regular expression syntax.

Use of Advanced Regular Expressions

Foswiki strives to support as much as possible of the Perl syntax for regular expressions. Be warned, though, that Foswiki only guarantees to support a subset of regular expression syntax, as documented in Foswiki:Development.RegularExpressions. This limitation is imposed by the third-party tools that Foswiki integrates with.

You can use more advanced features of Perl regular expressions, but your searches are not guaranteed to be supported on all Foswiki configurations, for example where a database store is in use and the database doesn't support the full Perl syntax for REs.

Related Links:


Related Topics: UserDocumentationCategory